How to check for repeating sequence in an integer

后端 未结 5 1588
夕颜
夕颜 2021-01-31 18:15

I have an alpha-numeric string and I want to check for pattern repetition in it just for the integers. And they should be continuous.

Example

5条回答
  •  后悔当初
    2021-01-31 18:55

    I am not sure if you are familiar with RegularExpressions (RegEx) but this code works

    String str = "12341234qwe";
    String rep = str.replaceAll(".*(.+)\\1.*","$1");
    if (rep.equals(str))
        System.out.println(str+" has no repition");
    else
        System.out.println(str+" has repition "+rep);
    str = "1234qwe1234";
    rep = str.replaceAll(".*(.+)\\1.*","$1");
    if (rep.equals(str))
        System.out.println(str+" has no repition");
    else
        System.out.println(str+" has repition "+rep);
    

    Here is tutorial: http://docs.oracle.com/javase/tutorial/essential/regex/

提交回复
热议问题