markvgti
markvgti

Reputation: 4619

Same regex works in one script, doesn't work in another

I have a simple regex ('(\b) +-{1,} +(\b)') that is included in a script (txt2tex.py ; line numbers 17 and 48 are the important ones) and can be given to another script (multilineRegex.py ; simply takes a regex to match against as it's 1st argument, replacement text as 2nd argument, and file name as 3rd) from the command line.

The regex doesn't match a given input text when evaluated inside txt2tex.py, but when I copy and paste it to the command line and invoke multilineRegex.py as follows, it works as expected:

multilineRegex.py '(\b) +-{1,} +(\b)' '---' input.txt

I noticed this discrepancy as I was playing around with regexes. Any ideas as to what I am doing wrong?

Used the following line to verify this behaviour:

Here's something - arbit for you to think of.

Details:

Upvotes: 0

Views: 95

Answers (1)

L3viathan
L3viathan

Reputation: 27323

That's because your shell interprets the \bs as control characters (backspace). You can demonstrate that by executing:

$ echo 'hello\bworld'
hellworld

When giving the regex to your script, escape it with an additional backslash:

multilineRegex.py '(\\b) +-{1,} +(\\b)' '---' input.txt

Upvotes: 1

Related Questions