How to represent a fix number of repeats in regular expression?

后端 未结 4 1567
北荒
北荒 2021-02-01 12:06

I am wondering if there is a better to represent a fix amount of repeats in a regular expression. For example, if I just want to match exactly 14 letters/digits, I am using

4条回答
  •  臣服心动
    2021-02-01 12:43

    The finite repetition syntax uses {m,n} in place of star/plus/question mark.

    From java.util.regex.Pattern:

    X{n}      X, exactly n times
    X{n,}     X, at least n times
    X{n,m}    X, at least n but not more than m times
    

    All repetition metacharacter have the same precedence, so just like you may need grouping for *, +, and ?, you may also for {n,m}.

    • ha* matches e.g. "haaaaaaaa"
    • ha{3} matches only "haaa"
    • (ha)* matches e.g. "hahahahaha"
    • (ha){3} matches only "hahaha"

    Also, just like *, +, and ?, you can add the ? and + reluctant and possessive repetition modifiers respectively.

        System.out.println(
            "xxxxx".replaceAll("x{2,3}", "[x]")
        ); "[x][x]"
    
        System.out.println(
            "xxxxx".replaceAll("x{2,3}?", "[x]")
        ); "[x][x]x"
    

    Essentially anywhere a * is a repetition metacharacter for "zero-or-more", you can use {...} repetition construct. Note that it's not true the other way around: you can use finite repetition in a lookbehind, but you can't use * because Java doesn't officially support infinite-length lookbehind.

    References

    • regular-expressions.info/Repetition

    Related questions

    • Difference between .* and .*? for regex
    • regex{n,}? == regex{n}?
    • Using explicitly numbered repetition instead of question mark, star and plus
      • Addresses the habit of some people of writing a{1}b{0,1} instead of ab?

提交回复
热议问题