Reputation: 1915
I am trying to match all the values 1
to 18
from string 24-15-7-49-63-2
using regex. I used regex before for general purpose but I don't have any idea how to do it.
Upvotes: 0
Views: 122
Reputation: 12596
The tricky thing is that you cannot easily define ranges with regexes. But this might do what you want:
\b([1-9]|1[0-8])\b
You can see it in action here: http://regexr.com?2v8jj
Here's an example in java:
String text = "24-15-7-49-63-2";
String pattern = "\\b([1-9]|1[0-8])\\b";
Pattern compiledPattern = Pattern.compile(pattern);
Matcher matcher = compiledPattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
Outputs:
15
7
2
Edit: Based on the comment you can get unique matches using this pattern:
\b([1-9]|1[0-8])\b(?!.*\b\1\b.*)
In action: http://regexr.com?2v8kh
Upvotes: 4