tags:

views:

69

answers:

3

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

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

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.

See the documentation for the re module.

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

Mark Byers
oh so \A == ^ and \Z == $
Blankman
@Blankman: They are equivalent if the input string is a single line.
Mark Byers
Not quite: **^** signifies the start of a *line*, while **$** signifies its end.
FK82
Not necessarily - "start of string" and "start of line" may not be equivalent - `'Line one \nLine two'`
nearlymonolith
A: 

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.

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.

FK82
A: 

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

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