user253956
user253956

Reputation: 323

How to ignore \n in regular expressions in python?

So i have a regex telling if a number is integer.

regex = '^(0|[1-9][0-9]*)$'

import re
bool(re.search(regex, '42\n'))

returns True, and it is not supposed to? Where does the problem come from ?

Upvotes: 1

Views: 5841

Answers (4)

David Z
David Z

Reputation: 131550

From the documentation:

'$'

Matches the end of the string or just before the newline at the end of the string

Try \Z instead.

Also, any time you find yourself writing a regular expression that starts with ^ or \A and ends with $ or \Z, if your intent is to only match the entire string, you should probably use re.fullmatch() instead of re.search() (and omit the boundary markers from the regex). Or if you're using a version of Python that's too old to have re.fullmatch(), (you really need to upgrade but) you can use re.match() and omit the beginning-of-string boundary marker.

Upvotes: 3

Tomek Wyderka
Tomek Wyderka

Reputation: 1465

Yeah, that $ matching one \n before the end is kind of trap/inconsistency. Check out my list of regex traps for python: http://www.cofoh.com/advanced-regex-tutorial-python/traps

Upvotes: 0

Anjali
Anjali

Reputation: 245

The regex in the question matches ->start of line, numbers and end of line. And the given string matches that, thats why it is returning true. If you want it to return False when there is a number present, you can use "!" to indicate NOT.

Refer https://docs.python.org/2/library/re.html

regex = '!(0|[1-9][0-9]*)$'
bool(re.search(regex, '42\n')) => (Returns false)

Upvotes: 0

Nacho
Nacho

Reputation: 71

regex ahould be regex = '\b^(0|[1-9][0-9]*)$\b'

Upvotes: 0

Related Questions