Reputation: 4609
I am trying to come up with the regex to find strings matching the following pattern:
(someNumber - someNumber) With the parenthesis included.
I tried:
"\\([1-9]*-[1-9]*\\)"
but that doesn't seem to work.
I also need to match:
The letter W or L followed by (someNumber - someNumber) With the parenthesis included.
I tried to use the same pattern above, slightly modified, but again, no luck:
"W|L \\([1-9]*-[1-9]*\\)"
Any help would be appreciated
Upvotes: 0
Views: 285
Reputation: 3454
Further to blueygh2's answer, your regex will fail if the numbers contain zeroes. My guess is you want to avoid leading zeroes, in which case use [1-9]\d*
(or [1-9][0-9]*
). If you want to allow the numbers to equal 0
but otherwise avoid leading zeroes, do ([1-9]\d*|0)
.
Upvotes: 0
Reputation: 1538
Include W|L in parentheses:
(W|L)
If you want to include space characters before and after the minus, add \s or a space before and after -
"((W|L)\\s)?\\([1-9]*\\s-\\s[1-9]*\\)"
If you already know that there will be at least one digit, use +
instead of *
, as *
matches zero or more, whereas +
matches 1 or more.
The pattern given above matches with and without a W or L in front.
Here's a pattern that matches with and without space around the -
and with or without W or L in front. Additionally, it also captures numbers containing 0
, which you excluded in your original regular expression.
"((W|L)\\s)?\\(\\d+\\s?-\\s?\\d+\\)"
Upvotes: 1