Reputation: 135
I need a regex that give me word after another word. For example:
street address Maria and another st. lohberg and give me another av. darrwerg and another st example
the desired output would be:
maria,lohberg,darrwerd,example
Maybye if there was a regex that could solve this situation: address: herman poortstrat this is text but not address
give me: herman poortstrat
But it is more complicated than that in my opinion.
I have the following regex: (?<=\bstreet\s)(\w+)
But it gives me only address after street with whitespace.
My regex give me text after "street" so it could be one of solutions but i need more words like: "av.","av","av ","address"
and other combinations.
Generally I want to get from text only the street name and nothing else. Not the street and its number only the street. I'm not good in regex yet, so i'm asking for your help.
Upvotes: 0
Views: 736
Reputation: 626699
You need to use
\b(?:street\s+address|(?:av|st)\b\.?)\s*(\w+)
See the regex demo
Details
\b
- word boundary(?:street\s+address|(?:av|st)\b\.?)
- a non-capturing group matching
street\s+address
- street
, 1+ whitespaces, address
|
- or(?:av|st)\b\.?
- av
or st
as whole words followed with an optional dot\s*
- 0 or more whitespaces(\w+)
- Group 1: one or more word charsString s = "street address Maria and another st. lohberg and give me another av. darrwerg and another st example";
Pattern p = Pattern.compile("\\b(?:street\\s+address|(?:av|st)\\b\\.?)\\s*(\\w+)", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s);
while (m.find()){
System.out.println("Value: " + m.group(1));
}
Results:
Value: Maria
Value: lohberg
Value: darrwerg
Value: example
Upvotes: 2
Reputation: 5341
This will give you the words after address, st. or av. You can tweak it for you needs
public static void main(String[] args) {
String str = "street address Santa Maria and another st. lohberg and give me another av. darrwerg and another st street";
Pattern p = Pattern.compile("((address|st\\.|av\\.)\\s)(\\w+?\\s)");
Matcher m = p.matcher(str);
List<String> streets = new ArrayList<String>();
while (m.find()) {
streets.add(m.group(3));
}
System.out.println(streets);
}
Upvotes: 2