如何将整个字符串与regex匹配?

时间:2023-01-14 14:05:37

I need a regex that will only find matches where the entire string matches my query.

我需要一个regex,它将只查找与整个字符串匹配的匹配项。

For instance if I do a search for movies with the name "Red October" I only want to match on that exact title (case insensitive) but not match titles like "The Hunt For Red October". Not quite sure I know how to do this. Anyone know?

例如,如果我搜索名为“Red October”的电影,我只想匹配那个确切的标题(不区分大小写),而不是匹配像“the Hunt For Red October”这样的标题。我不知道该怎么做。有人知道吗?

Thanks!

谢谢!

8 个解决方案

#1


77  

Try the following regular expression:

试试下面的正则表达式:

^Red October$

By default, regular expressions are case sensitive. The ^ marks the start of the matching text and $ the end.

默认情况下,正则表达式是区分大小写的。^标志匹配文本的开始和结束美元。

#2


24  

Generally, and with default settings, ^ and $ anchors are a good way of ensuring that a regex matches an entire string.

一般来说,默认设置,^和$锚是一个好方法确保一个正则表达式匹配整个字符串。

A few caveats, though:

不过,一些注意事项:

If you have alternation in your regex, be sure to enclose your regex in a non-capturing group before surrounding it with ^ and $:

如果有变更你的正则表达式,一定要附上你的正则表达式在一群无周围^和$:

^foo|bar$

is of course different from

当然不同吗

^(?:foo|bar)$

Also, ^ and $ can take on a different meaning (start/end of line instead of start/end of string) if certain options are set. In text editors that support regular expressions, this is usually the default behaviour. In some languages, especially Ruby, this behaviour cannot even be switched off.

^和$可以不同的含义(开始/结束线而不是字符串的开始/结束)如果某些选项设置。支持正则表达式的文本编辑器,这通常是默认的行为。在某些语言中,尤其是Ruby,这种行为甚至不能被关闭。

Therefore there is another set of anchors that are guaranteed to only match at the start/end of the entire string:

因此,还有一组锚保证只在整个字符串的开始/结束时匹配:

\A matches at the start of the string.

在字符串开始时进行匹配。

\Z matches at the end of the string or before a final line break.

\Z会在字符串的末尾或最后一行中断之前匹配。

\z matches at the very end of the string.

\z在字符串的末尾匹配。

But not all languages support these anchors, most notably JavaScript.

但是并不是所有的语言都支持这些锚点,尤其是JavaScript。

#3


15  

Use the ^ and $ modifiers to denote where the regex pattern sits relative to the start and end of the string:

使用^和$修饰符来表示的正则表达式模式是相对于字符串的开始和结束:

Regex.Match("Red October", "^Red October$"); // pass
Regex.Match("The Hunt for Red October", "^Red October$"); // fail

#4


8  

You need to enclose your regex in ^ (start of string) and $ (end of string):

你需要将你的regex ^(字符串)开始和结束(字符串):美元

^Red October$

#5


2  

I know that this may be a little late to answer this, but maybe it will come handy for someone else.

我知道回答这个问题可能有点晚了,但也许对其他人来说会很方便。

Simplest way:

最简单的方法:

var someString = "...";
var someRegex = "...";
var match = Regex.Match(someString , someRegex );
if(match.Success && match.Value.Length == someString.Length){
    //pass
} else {
    //fail
}

#6


1  

Sorry, but that's a little unclear.

抱歉,这有点不清楚。

From what i read, you want to do simple string compare. You don't need regex for that.

根据我读到的内容,您需要做简单的字符串比较。你不需要正则表达式。

string myTest = "Red October";
bool isMatch = (myTest.ToLower() == "Red October".ToLower());
Console.WriteLine(isMatch);
isMatch = (myTest.ToLower() == "The Hunt for Red October".ToLower());

#7


0  

Matching the entire string can be done with ^ and $ as said in previous answers.

匹配整个字符串可以用^和$表示在之前的答案。

To make the regex case insensitive, you can implement a hack that involves character classes. A lot of character classes.

为了使regex大小写不敏感,您可以实现一个包含字符类的hack。很多角色类。

^[Rr][Ee][Dd] [Oo][Cc][Tt][Oo][Bb][Ee][Rr]$

#8


-1  

You can do it like this Exemple if i only want to catch one time the letter minus a in a string and it can be check with myRegex.IsMatch()

你可以像这个例子一样如果我只想抓住一次字符串中的字母- a可以用myregex。ismatch()进行检查

^[^e][e]{1}[^e]$

^ ^[e][e]{ 1 }[^ e]美元

#1


77  

Try the following regular expression:

试试下面的正则表达式:

^Red October$

By default, regular expressions are case sensitive. The ^ marks the start of the matching text and $ the end.

默认情况下,正则表达式是区分大小写的。^标志匹配文本的开始和结束美元。

#2


24  

Generally, and with default settings, ^ and $ anchors are a good way of ensuring that a regex matches an entire string.

一般来说,默认设置,^和$锚是一个好方法确保一个正则表达式匹配整个字符串。

A few caveats, though:

不过,一些注意事项:

If you have alternation in your regex, be sure to enclose your regex in a non-capturing group before surrounding it with ^ and $:

如果有变更你的正则表达式,一定要附上你的正则表达式在一群无周围^和$:

^foo|bar$

is of course different from

当然不同吗

^(?:foo|bar)$

Also, ^ and $ can take on a different meaning (start/end of line instead of start/end of string) if certain options are set. In text editors that support regular expressions, this is usually the default behaviour. In some languages, especially Ruby, this behaviour cannot even be switched off.

^和$可以不同的含义(开始/结束线而不是字符串的开始/结束)如果某些选项设置。支持正则表达式的文本编辑器,这通常是默认的行为。在某些语言中,尤其是Ruby,这种行为甚至不能被关闭。

Therefore there is another set of anchors that are guaranteed to only match at the start/end of the entire string:

因此,还有一组锚保证只在整个字符串的开始/结束时匹配:

\A matches at the start of the string.

在字符串开始时进行匹配。

\Z matches at the end of the string or before a final line break.

\Z会在字符串的末尾或最后一行中断之前匹配。

\z matches at the very end of the string.

\z在字符串的末尾匹配。

But not all languages support these anchors, most notably JavaScript.

但是并不是所有的语言都支持这些锚点,尤其是JavaScript。

#3


15  

Use the ^ and $ modifiers to denote where the regex pattern sits relative to the start and end of the string:

使用^和$修饰符来表示的正则表达式模式是相对于字符串的开始和结束:

Regex.Match("Red October", "^Red October$"); // pass
Regex.Match("The Hunt for Red October", "^Red October$"); // fail

#4


8  

You need to enclose your regex in ^ (start of string) and $ (end of string):

你需要将你的regex ^(字符串)开始和结束(字符串):美元

^Red October$

#5


2  

I know that this may be a little late to answer this, but maybe it will come handy for someone else.

我知道回答这个问题可能有点晚了,但也许对其他人来说会很方便。

Simplest way:

最简单的方法:

var someString = "...";
var someRegex = "...";
var match = Regex.Match(someString , someRegex );
if(match.Success && match.Value.Length == someString.Length){
    //pass
} else {
    //fail
}

#6


1  

Sorry, but that's a little unclear.

抱歉,这有点不清楚。

From what i read, you want to do simple string compare. You don't need regex for that.

根据我读到的内容,您需要做简单的字符串比较。你不需要正则表达式。

string myTest = "Red October";
bool isMatch = (myTest.ToLower() == "Red October".ToLower());
Console.WriteLine(isMatch);
isMatch = (myTest.ToLower() == "The Hunt for Red October".ToLower());

#7


0  

Matching the entire string can be done with ^ and $ as said in previous answers.

匹配整个字符串可以用^和$表示在之前的答案。

To make the regex case insensitive, you can implement a hack that involves character classes. A lot of character classes.

为了使regex大小写不敏感,您可以实现一个包含字符类的hack。很多角色类。

^[Rr][Ee][Dd] [Oo][Cc][Tt][Oo][Bb][Ee][Rr]$

#8


-1  

You can do it like this Exemple if i only want to catch one time the letter minus a in a string and it can be check with myRegex.IsMatch()

你可以像这个例子一样如果我只想抓住一次字符串中的字母- a可以用myregex。ismatch()进行检查

^[^e][e]{1}[^e]$

^ ^[e][e]{ 1 }[^ e]美元