0

I want to match a string that has alphanumerics and some special characters but not the newline. But, whenever my string has a newline, it matches the newline character as well. I checked document for some flags but none of them looked relevant.

The following is a sample code in Python 3.6.2 REPL

>>> import re
>>> s = "do_not_match\n"
>>> p = re.compile(r"^[a-zA-Z\+\-\/\*\%\_\>\<=]*$")
>>> p.match(s)
<_sre.SRE_Match object; span=(0, 12), match='do_not_match'>

The expected result is that it shouldn't match as I have newline at the end.

https://regex101.com/r/qyRw5s/1

I am a bit confused on what I am missing here.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Ömer
  • 1,666
  • 2
  • 18
  • 33

1 Answers1

3

The problem is that $ matches at the end of the string before the newline (if any).

If you don't want to match the newline at the end, use \Z instead of $ in your regex.

See the re module's documentation:

'$'
Matches the end of the string or just before the newline at the end of the string,

\Z
Matches only at the end of the string.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378