Fire assassin
Fire assassin

Reputation: 81

Python, Matching Regular Expressions

I'm practicing regular expression with python using regex101.com. But i'm having trouble trying to meet these conditions: A valid string starts with (1) a positive + or negative - symbol, directly followed by (2) a integer, followed by (3) one or more spaces, followed by (4) one of the following operators: + - * / or ˆ, followed by (5) one or more spaces, followed by (6) a positive or negative symbol, directly followed by (7) an integer.

Match:
+6 * -2
+99 / +15
-100 ^   -2
-13 - -33
+12  -  +102
+3 + +3
-3 + -3

Not a Match:
- 15/-12
-19 + 12
+12  -  +102
3 + 3

What i have written below, meets both what's matched and not matched:

r'^[-+]?.|[0-9]|[ ]{1,}[+,-,*,/,^]|[ ]{1,}[-+]|[0-9].$'

anyone sees the issue? greatly appreciated.

Upvotes: 0

Views: 89

Answers (2)

Błotosmętek
Błotosmętek

Reputation: 12927

You put the alternative symbol (|) between your terms, where it shouldn't be. Besides, you put commas within a character class [+,-,*,/,^] - this simply means that comma is also one of matched characters. Also note that within this class: [+*/^-] order is somewhat important - - must come first or last, otherwise it is interpreted as a character range, while ^ can't be first or it is interpreted as negation. The correct regexp fitting your criteria is:

r'^[-+][0-9]+[ ]+[+*/^-][ ]+[-+][0-9]+$'

and it can be further shortened by replacing [0-9] with \d.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522752

I would use:

[+-]\d+\s+[*/^+-]\s+[+-]\d+

Sample script:

inp = """Match:
+6 * -2
+99 / +15
-100 ^   -2
-13 - -33
+12  -  +102
+3 + +3
-3 + -3

Not a Match:
- 15/-12
-19 + 12
+12  -  +102
3 + 3"""

matches = re.findall(r'[+-]?\d+\s+[*/^+-]\s+[+-]?\d+', inp)
print(matches)

This prints:

['+6 * -2', '+99 / +15', '-100 ^   -2', '-13 - -33', '+12  -  +102', '+3 + +3', '-3 + -3',
 '+12  -  +102']

Note that this actually includes +12 - +102; there does not seem to be anything invalid about this sample, despite that you listed it in the non match set.

Here is an explanation of the regex pattern:

[+-]     match a leading + or - in front of each number operand
\d+      match one or more digits
\s+      one or more spaces (technically any whitespace character)
[*/^+-]  an operator
\s+      one or more spaces
[+-]     leading + or -
\d+      one or more digits

Upvotes: 2

Related Questions