Reputation: 38842
The content of my string (MY_STRING
)could be in the following format:
bla bla...this is the id of product bla bla:#31 5 2 0000 12please verify bla bla ...
or
bla bla...this is the id of product bla bla: #31 5 2 0000 12, please verify bla bla...
or
bla bla...this is the id of product bla bla: #31 5 2 0000 12 please verify bla bla...
I want to extract out the product ID from the string. The product ID in above example is #31 5 2 0000 12
The format of product ID is that it starts with # which is followed by random numbers (the length is unlimited), the spaces between numbers are arbitrary too.
My current code to extract out product ID is:
Pattern pattern = Pattern.compile("^#\\d+(\\s+\\d+)*$");
Matcher matcher = pattern.matcher(MY_STRING);
if(phoneNrMatcher.find()){
System.out.println(matcher.group(0));
}
But it does not work, could some one help me where goes wrong? Probably the regular expression?
NOTE:
-In my example the content before & after ID #31 5 2 0000 12 is arbitrary.
-product ID string always starts with # which is followed by a number immediately without space or other char
Upvotes: 1
Views: 89
Reputation: 48404
Try this:
String test = "bla bla...this is the tag id of product: #31 5 2 0000 12, please verify bla bla...";
// explanation of the Pattern:
// |starts with "#"
// | |directly followed by digits only
// | | |character class including digits or spaces
// | | | |ad lib (greedy quantifier)
Pattern pattern = Pattern.compile("#\\d+[\\d\\s]+");
Matcher matcher = pattern.matcher(test);
// using a while group here so you may have multiple matches
while (matcher.find()) {
System.out.println(matcher.group());
}
Output:
#31 5 2 0000 12
Explanation:
You don't need to mention the beginning or end of line in your Pattern in this case. Also, the Pattern in my example would allow you to find more than one id in the same String, provided they are separated by a character that is neither a space nor a digit.
Upvotes: 3
Reputation: 121702
You have the beginning and end of input anchors to your regex (^
and $
). Remove them!
The beginning of input anchor makes it so that the regex cannot match anywhere else than at the beginning of the input, as the name implies; the end of input anchor makes is so that... You get the picture. Other than that, the regex is fine.
(btw, you can just use .group()
, it's the same as .group(0)
)
(btw 2: if you have several numbers in one input, loop over m.find()
)
Upvotes: 1