how to find if a string contains numbers followed by a specific string
java, parsing, string
Solution
Use:
if ( str.matches(".*\\dst.*") )
`String#matches()` matches the regex pattern from beginning of the string to the end. The anchors `^` and `$` are implicit. So, you should use the pattern that matches the complete string.
Or, use `Pattern`, `Matcher` and `Matcher#find()` method, to search for a particular pattern anywhere in a string:
Matcher matcher = Pattern.compile("\\dst").matcher(str);
if (matcher.find()) {
// ok
}
Problem
I have a string like this: ``` String str = "Friday 1st August 2013" ``` I need to check: if the string contains "any number" followed by the "st" string, print "yes", else print "no". I tried: `if ( str.matches(".*\\dst") )` and `if ( str.matches(".*\\d.st") )` but it doesn't work. Any help?