Reputation: 2146
I need a regular expression which roughly combines the logic of
?[1-9][0-9]+\s+X|x
with
X|x+\s*+[1-9][0-9]*
Or in english: match a pattern of the letter X (capitalised or uncapitalised) preceded by an integer (single space optional) OR succeeded by an integer (single space optional)
Thanks.
P.S. The two separate regex above are just for illustration; haven't actually tested them.
Upvotes: 1
Views: 52
Reputation: 2017
This will work and matches specifically to your request:
((X|x)\s*\d+)|(\d+\s*(X|x))
Explanation:
(
(X|x) First character must be an 'X' or 'x'
\s* Second character can be zero or infinite amount of spaces
\d+ Third character has to be one or more digits
)
| or
(
\d+ First character has to be one or more digits
\s* Second character can be zero or infinite amount of spaces
(X|x) Third character must be an 'X' or 'x'
)
Upvotes: 1
Reputation: 467
You're nearly there, you need enclosing brackets to group as a capture group and to handle numbers other than those 2 digits long.
/([0-9]+\s{0,1}(X|x))|((X|x)\s{0,1}[0-9]+)/g
Paste it into http://regexr.com/ to try it out. I tested it with:
X9847
X 2645
4442 x
x 525521
5254X5541
221 X 266
Upvotes: 1
Reputation: 3036
You said single space optional
so i used \s?
. If you can have any amount of whitespace, replace \s
with \s*
Try using this:
\d+\s?(X|x)|\s?\d+
Upvotes: 1
Reputation: 10484
This does exactly what you want:
(\d\s?[xX]{1}|[xX]{1}\s?\d)
See http://regexr.com/.
Might not be the best regex.
Upvotes: 1