Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6688135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:22:10+00:00 2026-05-26T05:22:10+00:00

I am trying to make a regex in Java which could crudely be used

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:22:11+00:00Added an answer on May 26, 2026 at 5:22 am

    Make sure you use Matcher.find and not Matcher.matches.

    This works fine on my machine:

    String s = "Then someone decided to go to the high street for a drink";
    
    Pattern p = Pattern.compile("(\\w+\\s*(road|street|square|rd|st|sq)\\W+)");
    
    Matcher m = p.matcher(s);
    
    System.out.println(m.find());   // prints true
    System.out.println(m.group());  // prints "high street"
    

    You could also simplify the expression a little:

    \w+\s*(road|street|square|rd|st|sq)\W
    

    or

    \w+\s*(r(oa)?d|st(reet)?|sq(uare)?)\W
    

    (gives the same output as above)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a regex that would match this string: asf, Algeria, Wilaya
I am trying to make a regex that finds all names, url and phone
I am trying to make a regex which will match the following formats so
I'm trying to make a dynamic regex that matches a person's name. It works
I am trying to make simple regex that will check if a line is
I've been trying to make a Regex to match the charset of mime multipart
I'm trying to make a regex, that will return list words that begin with
I am trying to make a regex that will just look for and remove
I'm trying to make a regex which gets the part between two other things
Im trying to make the following thing : 1) using regex to match all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.