这个正则表达式是否意味着它必须以A开头,以Z结尾? re.search“\ A [0-9A-Za-z _-] + \ Z”

时间:2020-12-18 21:17:01

Does this regex mean it has to start with A, end with Z?

这个正则表达式是否意味着它必须以A开头,以Z结尾?

re.search("\A[0-9A-Za-z_-]+\Z", sometext)

3 个解决方案

#1


7  

No, those are anchors.

不,那些是锚。

\A means start of string, and \Z means end of string. Similarly ^ means start of line and $ means end of line.

\ A表示字符串的开头,\ Z表示字符串的结尾。类似地,^表示行的开头,$表示行的结尾。

See the documentation for the re module.

请参阅re模块的文档。

\A - Matches only at the start of the string.
\Z - Matches only at the end of the string.

\ A - 仅在字符串的开头匹配。 \ Z - 仅匹配字符串的末尾。

#2


1  

What is "it"?

它是什么”?

If you're talking about a string. Yes, it does: \A means beginning of a string , \Z means end of a string.

如果你在谈论一个字符串。是的,它确实:\ A表示字符串的开头,\ Z表示字符串的结尾。

If you are talking about a line (inside a string), you will have to insert boundary operators:

如果你在谈论一条线(在一个字符串内),你将不得不插入边界运算符:

"^[0-9A-Za-z_-]+$"

^ ("caret") specifies beginning of a line; $ ("dollar sign") specifies the end of a line.

^(“插入符号”)指定一行的开头; $(“美元符号”)指定行的结尾。

If you are talking about a word: no, it does not; you did not specify start or end of a word.

如果你在谈论一个词:不,它没有;你没有指定单词的开头或结尾。

#3


0  

Just remove the '\' and you will get what you want.

只需删除'\'即可获得所需内容。

"^A[0-9A-Za-z_-]+Z$"

#1


7  

No, those are anchors.

不,那些是锚。

\A means start of string, and \Z means end of string. Similarly ^ means start of line and $ means end of line.

\ A表示字符串的开头,\ Z表示字符串的结尾。类似地,^表示行的开头,$表示行的结尾。

See the documentation for the re module.

请参阅re模块的文档。

\A - Matches only at the start of the string.
\Z - Matches only at the end of the string.

\ A - 仅在字符串的开头匹配。 \ Z - 仅匹配字符串的末尾。

#2


1  

What is "it"?

它是什么”?

If you're talking about a string. Yes, it does: \A means beginning of a string , \Z means end of a string.

如果你在谈论一个字符串。是的,它确实:\ A表示字符串的开头,\ Z表示字符串的结尾。

If you are talking about a line (inside a string), you will have to insert boundary operators:

如果你在谈论一条线(在一个字符串内),你将不得不插入边界运算符:

"^[0-9A-Za-z_-]+$"

^ ("caret") specifies beginning of a line; $ ("dollar sign") specifies the end of a line.

^(“插入符号”)指定一行的开头; $(“美元符号”)指定行的结尾。

If you are talking about a word: no, it does not; you did not specify start or end of a word.

如果你在谈论一个词:不,它没有;你没有指定单词的开头或结尾。

#3


0  

Just remove the '\' and you will get what you want.

只需删除'\'即可获得所需内容。

"^A[0-9A-Za-z_-]+Z$"