java 正则表达式匹配一个或多个数字 1-9、一个 '|'、一个或多个 '*" 和零个或多个 ','
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5590328/
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
Regular Expression to match one or more digits 1-9, one '|', one or more '*" and zero or more ','
提问by vprasad
I'm new to regular expressions and I need to find a regular expression that matches one or more digits [1-9] only ONE '|' sign, one or more '*' sign and zero or more ',' sign.
我是正则表达式的新手,我需要找到一个正则表达式匹配一个或多个数字 [1-9] 只有一个 '|' 符号、一个或多个“*”符号和零个或多个“,”符号。
The string should not contain any other characters. This is what I have:
该字符串不应包含任何其他字符。这就是我所拥有的:
if(this.ruleString.matches("^[1-9|*,]*$"))
{
return true;
}
Is it correct?
Thanks,
Vinay
这是正确的吗?
谢谢,
维奈
回答by squawknull
Nope, try this:
不行,试试这个:
"^[1-9]+\|\*+,*$"
回答by MadBender
I think you should test separately for every type of symbols rather than write complex expression.
我认为您应该对每种类型的符号分别进行测试,而不是编写复杂的表达式。
First, test that you don't have invalid symbols - "^[0-9|*,]$"
首先,测试您没有无效符号 - "^[0-9|*,]$"
Then test for digits "[1-9]"
, it should match at least one.
然后测试数字"[1-9]"
,它应该至少匹配一个。
Then test for "\\|"
, "\\*"
and "\\,"
and check the number of matches.
然后测试"\\|"
,"\\*"
和"\\,"
检查匹配的数量。
If all test are passed then your string is valid.
如果所有测试均通过,则您的字符串有效。
回答by Francisco Alvarado
Please give us at least 10 possible matching strings of what you are looking to accept, and 10 of what you want to reject, and tell us if either this have to keep some sequence or its order doesn't matter. So we can make a reliable regex.
请给我们至少 10 个您希望接受的匹配字符串,以及 10 个您想拒绝的匹配字符串,并告诉我们这是否必须保持某种顺序或其顺序是否无关紧要。所以我们可以制作一个可靠的正则表达式。
By now, all I can offer is:
到现在为止,我所能提供的就是:
^[1-9]+\|{1}\*+,*$
This RegEx was tested against these sample strings, accepting them:
此 RegEx 已针对这些示例字符串进行了测试,并接受了它们:
56421|*****,,,
2|*********,,,
1|*
7|*,
18|****
123456789|*
12|********,,
1516332|**,,,
111111|*
6|*****,,,,
And it was tested against these sample strings, rejecting them:
它针对这些示例字符串进行了测试,拒绝了它们:
10|*,
2***525*|*****,,,
123456,15,22*66*****4|,,,*167
1|2*3,4,5,6*
,*|173,
|*,
||12211
12
1|,*
1233|54|***,,,,
I assume your given order is strict and all conditions apply at the same time.
我假设您给定的命令是严格的,并且所有条件同时适用。
回答by Beel
It looks like the pattern you need is
看起来你需要的模式是
n-n, one or more times seperated by commas
then a bar (|)
then n*n, one or more times seperated by commas.
nn,一次或多次以逗号分隔,
然后是一个横杠 (|),
然后是 n*n,一次或多次以逗号分隔。
Here is a regular expression for that.
这是一个正则表达式。
([1-9]{1}[0-9]*\-[0-9]+){1} (,[1-9]{1}[0-9]*\-[0-9]+)* \| ([1-9]{1}[0-9]*\*[0-9]+){1} (,[1-9]{1}[0-9]*\*[0-9]+)*
But it is so complex, and does not take into account the details, such as for the case of n-m, you want n less than m (I guess). And you likely want the same number of n-m before the bar, and x*y after the bar.
但是它太复杂了,没有考虑到细节,比如对于nm的情况,你希望n小于m(我猜)。并且您可能希望条形前的 nm 数和条形后的 x*y 数相同。
Depends whether you want to check the syntax completely or not. (I hope you do want to.)
取决于您是否要完全检查语法。(我希望你确实想要。)
Since this is so complex, it should be done with a set of code instead of a single regular expression.
由于这非常复杂,因此应该使用一组代码而不是单个正则表达式来完成。
回答by Jim Wolff
this regex should work
这个正则表达式应该可以工作
"^[1-9\|\*,-]*$"
- Assert position at the beginning of the string ?^?
- Match a single character present in the list below ?[1-9\|*,-]?
- Between zero and unlimited times, as many times as possible, giving back as needed (greedy) ??
- A character in the range between “1” and “9” ?1-9?
- A | character ?\|?
- A * character ?*?
- The character “,” ?,?
- The character “-” ?-?
- Assert position at the end of the string (or before the line break at the end of the string, if any) ?$?
- 断言字符串开头的位置 ?^?
- 匹配下面列表中的单个字符 ?[1-9\|*,-] ?
- 在零次和无限次之间,尽可能多次,根据需要回馈(贪婪)??
- 介于“1”和“9”之间的字符?1-9?
- 一个 | 字符 ?\|?
- 一个人物 ?*?
- 人物 ”,” ?,?
- 人物 ”-” ?-?
- 断言字符串末尾的位置(或字符串末尾的换行符之前,如果有的话) ?$?