在regex中,\w*是什么意思?

时间:2022-07-28 22:29:59

In Python. r^[\w*]$

在Python中。r ^(\ w *)美元

whats that mean?

这是什么意思?

6 个解决方案

#1


37  

Quick answer: Match a string consisting of a single character, where that character is alphanumeric (letters, numbers) an underscore (_) or an asterisk (*).

快速回答:匹配由单个字符组成的字符串,其中字符是字母数字(字母、数字)、下划线(_)或星号(*)。

Details:

细节:

  • The "\w" means "any word character" which usually means alphanumeric (letters, numbers, regardless of case) plus underscore (_)
  • “\w”的意思是“任何单词字符”,通常表示字母数字(字母、数字,不管什么情况),加上下划线(_)
  • The "^" "anchors" to the beginning of a string, and the "$" "anchors" To the end of a string, which means that, in this case, the match must start at the beginning of a string and end at the end of the string.
  • “^”“锚”一个字符串的开始,和“$”“锚”的一个字符串,这意味着,在本例中,字符串的匹配必须从头开始和结束的字符串。
  • The [] means a character class, which means "match any character contained in the character class".
  • []表示字符类,意思是“匹配字符类中包含的任何字符”。

It is also worth mentioning that normal quoting and escaping rules for strings make it very difficult to enter regular expressions (all the backslashes would need to be escaped with additional backslashes), so in Python there is a special notation which has its own special quoting rules that allow for all of the backslashes to be interpreted properly, and that is what the "r" at the beginning is for.

也值得一提的正常引用和转义规则字符串很难进入正则表达式(所有反斜杠需要保住了额外的反斜杠),所以在Python中有一个特殊的符号都有自己的特殊引用规则,允许所有反斜杠的正确解释,这就是开始的“r”。

Note: Normally an asterisk (*) means "0 or more of the previous thing" but in the example above, it does not have that meaning, since the asterisk is inside of the character class, so it loses its "special-ness".

注意:通常星号(*)的意思是“0或更多以前的东西”,但是在上面的示例中,它没有这个意思,因为星号在字符类中,所以它失去了它的“特殊性”。

For more information on regular expressions in Python, the two official references are the re module, the Regular Expression HOWTO.

关于Python中正则表达式的更多信息,两个官方引用是re模块,正则表达式HOWTO。

#2


1  

As exhuma said, \w is any word-class character (alphanumeric as Jonathan clarifies).

正如exhuma所说,\w是任何一个词类字符(如Jonathan所澄清的字母数字)。

However because it is in square brackets it will match:

但由于它在方括号中,它将匹配:

  1. a single alphanumeric character OR
  2. 一个字母数字字符或
  3. an asterisk (*)
  4. 星号(*)

So the whole regular expression matches:

所以整个正则表达式匹配:

  • the beginning of a line (^)
  • 一行的开头(^)
  • followed by either a single alphanumeric character or an asterisk
  • 后面跟着一个字母数字字符或星号
  • followed by the end of a line ($)
  • 后面跟着一行($)

so the following would match:

因此,下面将匹配:

blah
z  <- matches this line
blah

or

blah
* <- matches this line
blah

#3


0  

From the beginning of this line, "Any number of word characters (letter, number, underscore)" until the end of the line.

从这行开始,“任意数量的字字符(字母、数字、下划线)”直到行尾。

I am unsure as to why it's in square brackets, as circle brackets (e.g. "(" and ")") are correct if you want the matched text returned.

我不确定为什么它在方括号中,如圆括号。如果您希望返回匹配的文本,则“(”和“)”是正确的。

#4


0  

\w refers to 0 or more alphanumeric characters and the underscore. the * in your case is also inside the character class, so [\w*] would match all of [a-zA-Z0-9_*] (the * is interpreted literally)

\w指0或更多的字母数字字符和下划线。在您的例子中,*也在字符类中,因此[\w*]将匹配所有的[a-zA-Z0-9_*](*按字面解释)

See http://www.regular-expressions.info/reference.html

参见http://www.regular-expressions.info/reference.html

To quote:

引用:

\d, \w and \s --- Shorthand character classes matching digits, word characters, and whitespace. Can be used inside and outside character classes.

\d、\w和\s——匹配数字、单词字符和空格的简写字符类。可以在字符类内部和外部使用。

Edit corrected in response to comment

编辑更正以回应评论

#5


0  

As said above \w means any word. so you could use this in the context of below

如上所述,w代表任何单词。所以你可以在下面的上下文中使用这个

view.aspx?url=[\w]

which means you can have any word as the value of the "url=" parameter

这意味着您可以将任何单词作为“url=”参数的值?

#6


-1  

\w is equivalent to [a-zA-Z0-9_] I don't understand the * after it or the [] around it, because \w already is a class and * in class definitions makes no sense.

\w等同于[a- za - z0 -9_]我不理解它后面的*或它周围的[],因为\w已经是一个类,而在类定义中*是没有意义的。

#1


37  

Quick answer: Match a string consisting of a single character, where that character is alphanumeric (letters, numbers) an underscore (_) or an asterisk (*).

快速回答:匹配由单个字符组成的字符串,其中字符是字母数字(字母、数字)、下划线(_)或星号(*)。

Details:

细节:

  • The "\w" means "any word character" which usually means alphanumeric (letters, numbers, regardless of case) plus underscore (_)
  • “\w”的意思是“任何单词字符”,通常表示字母数字(字母、数字,不管什么情况),加上下划线(_)
  • The "^" "anchors" to the beginning of a string, and the "$" "anchors" To the end of a string, which means that, in this case, the match must start at the beginning of a string and end at the end of the string.
  • “^”“锚”一个字符串的开始,和“$”“锚”的一个字符串,这意味着,在本例中,字符串的匹配必须从头开始和结束的字符串。
  • The [] means a character class, which means "match any character contained in the character class".
  • []表示字符类,意思是“匹配字符类中包含的任何字符”。

It is also worth mentioning that normal quoting and escaping rules for strings make it very difficult to enter regular expressions (all the backslashes would need to be escaped with additional backslashes), so in Python there is a special notation which has its own special quoting rules that allow for all of the backslashes to be interpreted properly, and that is what the "r" at the beginning is for.

也值得一提的正常引用和转义规则字符串很难进入正则表达式(所有反斜杠需要保住了额外的反斜杠),所以在Python中有一个特殊的符号都有自己的特殊引用规则,允许所有反斜杠的正确解释,这就是开始的“r”。

Note: Normally an asterisk (*) means "0 or more of the previous thing" but in the example above, it does not have that meaning, since the asterisk is inside of the character class, so it loses its "special-ness".

注意:通常星号(*)的意思是“0或更多以前的东西”,但是在上面的示例中,它没有这个意思,因为星号在字符类中,所以它失去了它的“特殊性”。

For more information on regular expressions in Python, the two official references are the re module, the Regular Expression HOWTO.

关于Python中正则表达式的更多信息,两个官方引用是re模块,正则表达式HOWTO。

#2


1  

As exhuma said, \w is any word-class character (alphanumeric as Jonathan clarifies).

正如exhuma所说,\w是任何一个词类字符(如Jonathan所澄清的字母数字)。

However because it is in square brackets it will match:

但由于它在方括号中,它将匹配:

  1. a single alphanumeric character OR
  2. 一个字母数字字符或
  3. an asterisk (*)
  4. 星号(*)

So the whole regular expression matches:

所以整个正则表达式匹配:

  • the beginning of a line (^)
  • 一行的开头(^)
  • followed by either a single alphanumeric character or an asterisk
  • 后面跟着一个字母数字字符或星号
  • followed by the end of a line ($)
  • 后面跟着一行($)

so the following would match:

因此,下面将匹配:

blah
z  <- matches this line
blah

or

blah
* <- matches this line
blah

#3


0  

From the beginning of this line, "Any number of word characters (letter, number, underscore)" until the end of the line.

从这行开始,“任意数量的字字符(字母、数字、下划线)”直到行尾。

I am unsure as to why it's in square brackets, as circle brackets (e.g. "(" and ")") are correct if you want the matched text returned.

我不确定为什么它在方括号中,如圆括号。如果您希望返回匹配的文本,则“(”和“)”是正确的。

#4


0  

\w refers to 0 or more alphanumeric characters and the underscore. the * in your case is also inside the character class, so [\w*] would match all of [a-zA-Z0-9_*] (the * is interpreted literally)

\w指0或更多的字母数字字符和下划线。在您的例子中,*也在字符类中,因此[\w*]将匹配所有的[a-zA-Z0-9_*](*按字面解释)

See http://www.regular-expressions.info/reference.html

参见http://www.regular-expressions.info/reference.html

To quote:

引用:

\d, \w and \s --- Shorthand character classes matching digits, word characters, and whitespace. Can be used inside and outside character classes.

\d、\w和\s——匹配数字、单词字符和空格的简写字符类。可以在字符类内部和外部使用。

Edit corrected in response to comment

编辑更正以回应评论

#5


0  

As said above \w means any word. so you could use this in the context of below

如上所述,w代表任何单词。所以你可以在下面的上下文中使用这个

view.aspx?url=[\w]

which means you can have any word as the value of the "url=" parameter

这意味着您可以将任何单词作为“url=”参数的值?

#6


-1  

\w is equivalent to [a-zA-Z0-9_] I don't understand the * after it or the [] around it, because \w already is a class and * in class definitions makes no sense.

\w等同于[a- za - z0 -9_]我不理解它后面的*或它周围的[],因为\w已经是一个类,而在类定义中*是没有意义的。