[英]string — x y in REGEX
Quick question. 快速提问。 I have this string
我有这串
string -- xy
I manage to get the constants down 我设法降低常数
/(string) (--) (X) (Y)/
my problem is x y. 我的问题是x y。 X and Y can be between 1-999 there's no leading zeros so no need to check on that.
X和Y可以在1-999之间,没有前导零,因此无需检查。
string -- ([1-9]\d{0,2}) ([1-9]\d{0,2})
string -- matches the characters string -- literally (case sensitive)
1st Capturing group ([1-9]\d{0,2})
[1-9] match a single character present in the list below
1-9 a single character in the range between 1 and 9
\d{0,2} match a digit [0-9]
Quantifier: {0,2} Between 0 and 2 times, as many times as possible, giving back as needed [greedy]
matches the character literally
2nd Capturing group ([1-9]\d{0,2})
[1-9] match a single character present in the list below
1-9 a single character in the range between 1 and 9
\d{0,2} match a digit [0-9]
Quantifier: {0,2} Between 0 and 2 times, as many times as possible, giving back as needed [greedy]
Examples 例子
string -- 1 999 //matches
string -- 10 02 //does not match
string -- 011 222 //does not match
string -- 111 222 //matches
string -- 41 2 //matches
string -- 999 1 //matches
string -- 1 1 //matches
您可以使用否定的前瞻表示避免前导零(和零):
/(string) (--)(?!.* 0) (\d{1,3}) (\d{1,3})
For the number portion you should be checking [1-9][0-9]{0,2}
. 对于数字部分,您应该检查
[1-9][0-9]{0,2}
。 Otherwise you'll miss valid numbers such as 10
and 101
. 否则,您将错过有效数字,例如
10
和101
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.