在 Java 正则表达式中匹配“_”和“-”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2121546/
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-10-29 19:33:45  来源:igfitidea点击:

Matching '_' and '-' in java regexes

javaregex

提问by Pablo Fernandez

I had this regex in javathat matched either an alphanumeric character or the tilde (~)

我在java中有这个正则表达式匹配字母数字字符或波浪号 (~)

^([a-z0-9])+|~$

^([a-z0-9])+|~$

Now I have to add also the characters -and _I've tried a few combinations, neither of which work, for example:

现在我还必须添加字符,-并且_我尝试了几种组合,但都不起作用,例如:

^([a-zA-Z0-9_-])+|~$^([a-zA-Z0-9]|-|_)+|~$

^([a-zA-Z0-9_-])+|~$^([a-zA-Z0-9]|-|_)+|~$

Sample input strings that must match:

必须匹配的示例输入字符串:

woZOQNVddd

woZOQNVdd

00000

00000

ncnW0mL14-

ncnW0mL14-

dEowBO_Eu7

dEowBO_Eu7

7MyG4XqFz-

7MyG4XqFz-

A8ft-y6hDu ~

A8ft-y6hDu ~

Any clues / suggestion?

任何线索/建议?

回答by cletus

-is a special character within square brackets. It indicates a range. If it's not at either end of the regex it needs to be escaped by putting a \before it.

-是方括号内的特殊字符。它表示一个范围。如果它不在正则表达式的任何一端,则需要通过\在它之前放置 a 来进行转义。

It's worth pointing out a shortcut: \wis equivalent to [0-9a-zA-Z_]so I think this is more readable:

值得指出一个快捷方式:\w相当于[0-9a-zA-Z_]所以我认为这更具可读性:

^([\w-]+|~$

回答by danben

You need to escape the -, like \-, since it is a special character (the range operator). _is ok.

您需要转义-, like \-,因为它是一个特殊字符(范围运算符)。 _没问题。

So ^([a-z0-9_\-])+|~$.

所以^([a-z0-9_\-])+|~$

Edit: your last input String will not match because the regular expression you are using matches a string of alphanumeric characters (plus -and _) OR a tilde (because of the pipe). But not both. If you want to allow an optional tilde on the end, change to:

编辑:您的最后一个输入字符串将不匹配,因为您使用的正则表达式匹配一串字母数字字符(加号-_)或波浪号(由于管道)。但不能两者兼而有之。如果要在末尾允许可选波浪号,请更改为:

^([a-z0-9_\-])+(~?)$

^([a-z0-9_\-])+(~?)$

回答by Bill the Lizard

If you put the -first, it won't be interpreted as the range indicator.

如果你把第-一个,它不会被解释为范围指示器。

^([-a-zA-Z0-9_])+|~$

This matches all of your examples except the last one using the following code:

这与使用以下代码的除最后一个之外的所有示例相匹配:

String str = "A8ft-y6hDu ~";
System.out.println("Result: " + str.matches("^([-a-zA-Z0-9_])+|~$"));

That last example won't match because it doesn't fit your description. The regex will match any combination of alphanumerics, -, and _, OR a ~ character.

最后一个示例不匹配,因为它不符合您的描述。正则表达式将匹配字母数字、- 和 _ 或 ~ 字符的任意组合。