I am using java regex library to find a pattern “String_OneOrMoreDigits”. For example, “linenumber_1” or “linenumber_31” or “linenumber_456”. I am trying the following pattern assuming I will get string of type “linenumber_2” or “linenumber_44”. However, I just get the strings of type “linenumber_2”, it does not match more than one number at the end. How to go about matching such strings?
Pattern pattern = Pattern.compile("(linenumber_[0-9])|(linenumber_[0-9][0-9])");
No need for an alternation, just use a “one or more” qualifier on the
[0-9]:That will match “linenumber_1” and “linenumber_44” and “linenumber_12345984”. If you only want to match one or two digits, you can do that by being more explicit about the number of digits allowed: