这个正则表达式意味着什么?

时间:2022-05-11 15:45:03

In a recent interview I was asked to decipher this regex

在最近的一次采访中,我被要求破译这个正则表达式

^\^[^^]

Can you please help me with it. Also please provide some links where I can learn regex for interviews.

你能帮帮我吗?另外请提供一些链接,我可以在那里学习正则表达式的访谈。

4 个解决方案

#1


30  

It matches strings that begin with ^ followed by any character other than ^.

它匹配以^开头的字符串,后跟除^之外的任何字符。

So it would match:

所以它会匹配:

^foo
^b

but not

但不是

foo
^^b

Explanation:

说明:

Caret (^) is a regex meta character with two different meanings:

Caret(^)是一个具有两种不同含义的正则表元字符:

Outside the character class(1st use in your regex) it works as start anchor.

在角色类之外(在你的正则表达式中第一次使用)它作为起始锚点。

Inside the character class it acts like negator if used as the first character of the character class(3rd use in your regex).

如果用作字符类的第一个字符(在你的正则表达式中使用第三个字符),它在字符类中就像是否定义。

Preceding a regex with \ escapes it (makes it non-special). The 2nd use of ^ in your regex is escaped and it matches a literal ^ in the string.

在\前面加一个正则表达式逃脱它(使它非特殊)。你的正则表达式中^的第二次使用是转义的,它匹配字符串中的文字^。

Inside a character class a ^ which is not the first character of the character class is treated literally. So the 4th use in your regex is a literal ^.

在字符类中,字面上处理的字符不是字符类的第一个字符。所以你的正则表达式中的第四个用法是文字^。

Some more examples to make it clear:

还有一些例子可以说清楚:

  • ^a         : Matches string beginning with a
  • ^ a:匹配以a开头的字符串
  • ^ab       : Matches string beginning with a followed by b
  • ^ ab:匹配以a开头,后跟b的字符串
  • [a]       : Matches a string which has an a
  • [a]:匹配一个带有a的字符串
  • [^a]     : Matches a string which does not have an a
  • [^ a]:匹配没有a的字符串
  • ^a[^a] : Matches a string beginning with an a followed by any character other than a.
  • ^ a [^ a]:匹配以a开头的字符串,后跟除a以外的任何字符。

#2


3  

I'm testing this regex here however it does not seem to be valid.
The first ^ denotes the start of the line.
The first \ escapes the following \.
Thus the second "^" is not escaped Finally the first caret inside the square brackets [^ acts as the negation and second one ^] is not escaped as a result is not valid.

我在这里测试这个正则表达式,但它似乎没有效果。第一个^表示该行的开始。第一个\逃脱以下\。因此,第二个“^”不会被转义。最后,方括号内的第一个插入符号[^充当否定而第二个^]不会被转义,因为结果无效。

IMHO the correct regexp should be ^\^[^\^]
Guys, kindly confirm. Many thanks

恕我直言正确的正则表达式应该是^ \ ^ [^ \ ^]伙计们,请确认。非常感谢

#3


2  

Match beginning of line or string followed by a literal \ followed by the beginning of the line or string followed by any character that is not a space, return or new line character

匹配行或字符串的开头,后跟文字\后跟行或字符串的开头,后跟任何不是空格,返回或换行符的字符

#4


2  

The first ^ is the beginning of line.

第一个^是行的开头。

The second one is a literal character of ^ (\ is to escape the other usual meaning of ^)

第二个是^的文字字符(\是为了逃避^的其他通常含义)

The third one is to say

第三个是说

a class of characters which does not include the character ^

一类不包含字符^的字符

Some example to show using Ruby:

使用Ruby显示的一些示例:

ruby-1.9.2-p0 > "hello" =~ /^h/    # it found a match at position 0
 => 0 

ruby-1.9.2-p0 > "hello" =~ /^e/    # nil means can't find it
 => nil 

ruby-1.9.2-p0 > "he^llo" =~ /\^/   # found at position 2
 => 2 

ruby-1.9.2-p0 > "he^llo"[/[^^]*/]  # anything repeatedly but not including the ^ character
 => "he" 

#1


30  

It matches strings that begin with ^ followed by any character other than ^.

它匹配以^开头的字符串,后跟除^之外的任何字符。

So it would match:

所以它会匹配:

^foo
^b

but not

但不是

foo
^^b

Explanation:

说明:

Caret (^) is a regex meta character with two different meanings:

Caret(^)是一个具有两种不同含义的正则表元字符:

Outside the character class(1st use in your regex) it works as start anchor.

在角色类之外(在你的正则表达式中第一次使用)它作为起始锚点。

Inside the character class it acts like negator if used as the first character of the character class(3rd use in your regex).

如果用作字符类的第一个字符(在你的正则表达式中使用第三个字符),它在字符类中就像是否定义。

Preceding a regex with \ escapes it (makes it non-special). The 2nd use of ^ in your regex is escaped and it matches a literal ^ in the string.

在\前面加一个正则表达式逃脱它(使它非特殊)。你的正则表达式中^的第二次使用是转义的,它匹配字符串中的文字^。

Inside a character class a ^ which is not the first character of the character class is treated literally. So the 4th use in your regex is a literal ^.

在字符类中,字面上处理的字符不是字符类的第一个字符。所以你的正则表达式中的第四个用法是文字^。

Some more examples to make it clear:

还有一些例子可以说清楚:

  • ^a         : Matches string beginning with a
  • ^ a:匹配以a开头的字符串
  • ^ab       : Matches string beginning with a followed by b
  • ^ ab:匹配以a开头,后跟b的字符串
  • [a]       : Matches a string which has an a
  • [a]:匹配一个带有a的字符串
  • [^a]     : Matches a string which does not have an a
  • [^ a]:匹配没有a的字符串
  • ^a[^a] : Matches a string beginning with an a followed by any character other than a.
  • ^ a [^ a]:匹配以a开头的字符串,后跟除a以外的任何字符。

#2


3  

I'm testing this regex here however it does not seem to be valid.
The first ^ denotes the start of the line.
The first \ escapes the following \.
Thus the second "^" is not escaped Finally the first caret inside the square brackets [^ acts as the negation and second one ^] is not escaped as a result is not valid.

我在这里测试这个正则表达式,但它似乎没有效果。第一个^表示该行的开始。第一个\逃脱以下\。因此,第二个“^”不会被转义。最后,方括号内的第一个插入符号[^充当否定而第二个^]不会被转义,因为结果无效。

IMHO the correct regexp should be ^\^[^\^]
Guys, kindly confirm. Many thanks

恕我直言正确的正则表达式应该是^ \ ^ [^ \ ^]伙计们,请确认。非常感谢

#3


2  

Match beginning of line or string followed by a literal \ followed by the beginning of the line or string followed by any character that is not a space, return or new line character

匹配行或字符串的开头,后跟文字\后跟行或字符串的开头,后跟任何不是空格,返回或换行符的字符

#4


2  

The first ^ is the beginning of line.

第一个^是行的开头。

The second one is a literal character of ^ (\ is to escape the other usual meaning of ^)

第二个是^的文字字符(\是为了逃避^的其他通常含义)

The third one is to say

第三个是说

a class of characters which does not include the character ^

一类不包含字符^的字符

Some example to show using Ruby:

使用Ruby显示的一些示例:

ruby-1.9.2-p0 > "hello" =~ /^h/    # it found a match at position 0
 => 0 

ruby-1.9.2-p0 > "hello" =~ /^e/    # nil means can't find it
 => nil 

ruby-1.9.2-p0 > "he^llo" =~ /\^/   # found at position 2
 => 2 

ruby-1.9.2-p0 > "he^llo"[/[^^]*/]  # anything repeatedly but not including the ^ character
 => "he"