[英]How to match the whole string in RegEx?
I'm want to have two patterns: 我想要两种模式:
1. "number/number number&chars"
2. "number_number"
1. "\d/\d \s"
2. "\d_\d"
These don't really work. 这些实际上不起作用。 For example the second one also matches "asdf894343_84939".
例如,第二个也匹配“ asdf894343_84939”。 How do I force the pattern to match the WHOLE string?
如何强制模式匹配WHOLE字符串?
You need to use the start-of-line ^
and end-of-line $
characters; 您需要使用行首
^
和行尾$
字符; for example, for your second pattern, use ^\\d_\\d$
. 例如,对于第二种模式,请使用
^\\d_\\d$
。
You colud use the '\\A' and '\\Z' flags - 您使用'\\ A'和'\\ Z'标志进行竞争-
Regex.IsMatch(subjectString, @"\A\d_\d\Z");
\\A Matches the position before the first character in a string.
\\ A匹配字符串中第一个字符之前的位置。 Not affected by the MultiLine setting
不受多行设置的影响
\\Z Matches the position after the last character of a string.
\\ Z匹配字符串的最后一个字符之后的位置。 Not affected by the MultiLine setting.
不受“多行”设置的影响。
From - http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet 来自-http: //www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet
For second task(number_number) you can use [^a-zA-Z]\\d.*_\\d.* , in you example asdf894343_84939, you get 894343_84939, or if you want to get only one digit - remove .* after \\d. 对于第二个任务(数字编号),您可以使用[^ a-zA-Z] \\ d。* _ \\ d。* ,在您的示例asdf894343_84939中,您将获得894343_84939,或者如果您仅想获得一位数字,请在之后删除。* \\ d。
In your first task, you also may use \\d.*/\\d[^\\s] , for example, if you have 34/45 sss - you get 34/45. 在第一个任务中,您还可以使用\\ d。* / \\ d [^ \\ s] ,例如,如果您有34/45 sss-您将获得34/45。 If you want to get result from whole string you must use in you pattern: ^your pattern$
如果要从整个字符串中获取结果,则必须在模式中使用:^ your pattern $
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.