正则表达式查找AA,ABC,ABCD等

本文介绍了六个Java方法,通过正则表达式搜索输入的CharSequence中特定模式的重复字符。方法包括查找连续重复数字、非连续重复数字和特定模式如ABAB、AAA等。每个方法都展示了如何使用Pattern和Matcher进行匹配并返回起始位置和长度。
摘要由CSDN通过智能技术生成
public static int[]  indexAAA(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d)\\1{2}");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 3};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexAAAA(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d)\\1{3}");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 4};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexAABB(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d)\\1(\\d)\\2");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 4};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexABBA(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d)((?!\\\\1)\\d)\\2\\1");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 4};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexABAB(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d\\d)\\1");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 4};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexAAAB(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d)\\1\\1((?!\\1)\\d)");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 4};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexAAB(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d)\\1((?!\\1)\\d)");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 3};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexABC(final CharSequence input) {
    Pattern p = Pattern.compile("((?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2}|(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){2})\\d");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 3};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexABCD(final CharSequence input) {
    Pattern p = Pattern.compile("((?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){3}|(?:0(?=9)|9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3})\\d");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 4};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexAAAAB(final CharSequence input) {
    Pattern p = Pattern.compile("(\\d)\\1\\1\\1((?!\\1)\\d)");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 5};
    } else {
        return new int[]{-1, -1};
    }
}

public static int[] indexABCABC(final CharSequence input) {
    Pattern p = Pattern.compile("((?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2}\\d)\\1");
    Matcher m = p.matcher(input);
    Log.i(TAG, m.toString());
    if (m.find()) {
        return new int[]{m.start(), 6};
    } else {
        return new int[]{-1, -1};
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值