I am trying to make a regex in Java which could crudely be used to match certain street names. I want to make it so that given the following string:
Then someone decided to go to the high street for a drink
The term “high street” would be matched. So that is the preceeding word and the word “street” to get the street name. I have tried something like this:
Pattern.compile("(\\w+\\s*(road|street|square|rd|st|sq)\\W+)");
But this is failing, it seems that Java wants to match the whole sentence, but I am just interested in a few words. I have tried a few reluctant quantifiers as well, but nothing seems to work.
Any help/suggestions will be much appreciated. Thanks!
Make sure you use
Matcher.findand notMatcher.matches.This works fine on my machine:
You could also simplify the expression a little:
or
(gives the same output as above)