The Desired Results
Suppose that we want a regex to match a sum of two variables.
x + y
length + width // no underscores in this example
rectan_width + rectan_height // spaces around `+`
rectan_width+rectan_height // no spaces around `+`
Attempt at a Functioning Regular Expression
Here is what I have so far…
(?<=s|^|b)(?:[_/]b|b[_/]|d*.?d+|[A-Za-z0-9]|([A-Za-z0-9]+))+(?=s|$|b) *+ *(?<=s|^|b)(?:[_/]b|b[_/]|d*.?d+|[A-Za-z0-9]|([A-Za-z0-9]+))+(?=s|$|b)
The demonstration can be viewed here.
String Literal |
Supposed to Match? |
Does the Demonstration Match? |
---|---|---|
X+Y |
Supposed to Match |
it matches |
X + Y |
Supposed to Match |
it matches |
APPLE+ORANGE |
Supposed to Match |
it matches |
apple+orange |
Supposed to Match |
it matches |
apple + orange |
Supposed to Match |
it matches |
rock1 + rock2 |
Supposed to Match |
it matches |
rectan_width + rectange_height |
Supposed to Match |
FAILS TO MATCH UNDERSCORES |