Java 如何在正则表达式中表示固定数量的重复?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3261581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:49:36  来源:igfitidea点击:

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

javaregex

提问by Ken

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 ^\w\w\w\w\w\w\w\w\w\w\w\w\w\w$which will match a word like UNL075BE499135and not match UNL075BE499135AAAis there a handy way to do it? In am currently doing it in java but I guess this may apply to other language as well. Thanks in advance.

我想知道在正则表达式中是否有更好的方法来表示固定数量的重复。例如,如果我只想精确匹配 14 个字母/数字,我正在使用^\w\w\w\w\w\w\w\w\w\w\w\w\w\w$哪个匹配单词 likeUNL075BE499135和 not matchUNL075BE499135AAA有没有方便的方法来做到这一点?我目前正在用 java 做这件事,但我想这也可能适用于其他语言。提前致谢。

采纳答案by shookster

For Java:

对于 Java:

Quantifiers documentation

量词文档

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

X,正好 n 次:X{n}
X,至少 n 次:X{n,}
X,至少 n 但不超过 m 次:X{n,m}

回答by eldarerathis

^\w{14}$in Perl and any Perl-style regex.

^\w{14}$在 Perl 和任何 Perl 风格的正则表达式中。

If you want to learn more about regular expressions - or just need a handy reference - the Wikipedia Entry on Regular Expressionsis actually pretty good.

如果您想了解有关正则表达式的更多信息 - 或者只是需要一个方便的参考 - Wikipedia Entry on Regular Expressions实际上非常好。

回答by Jano González

In Java create the pattern with Pattern p = Pattern.compile("^\\w{14}$");for further information see the javadoc

在 Java 中创建模式以Pattern p = Pattern.compile("^\\w{14}$");获取更多信息,请参阅 javadoc

回答by polygenelubricants

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

有限重复语法用于{m,n}代替星号/加号/问号。

From java.util.regex.Pattern:

来自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}.

所有重复元字符具有相同的优先级,所以就像您可能需要为分组*+?,你也可以{n,m}

  • ha*matches e.g. "haaaaaaaa"
  • ha{3}matches only "haaa"
  • (ha)*matches e.g. "hahahahaha"
  • (ha){3}matches only "hahaha"
  • ha*匹配例如 "haaaaaaaa"
  • ha{3}只匹配 "haaa"
  • (ha)*匹配例如 "hahahahaha"
  • (ha){3}只匹配 "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.

基本上任何地方 a*是“零个或多个”的重复元字符,您都可以使用{...}重复构造。请注意,反过来就不是这样了:您可以在后视中使用有限重复,但不能使用,*因为 Java 不正式支持无限长后视。

References

参考

Related questions

相关问题