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

  • Home
  • SEARCH
  • 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 8143183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:02:52+00:00 2026-06-06T13:02:52+00:00

I am trying to write a regular expression, in java, that matches words and

  • 0

I am trying to write a regular expression, in java, that matches words and hyphenated words. So far I have:

Pattern p1 = Pattern.compile("\\w+(?:-\\w+)",Pattern.CASE_INSENSITIVE);
Pattern p2 = Pattern.compile("[a-zA-Z0-9]+",Pattern.CASE_INSENSITIVE);
Pattern p3 = Pattern.compile("(?<=\\s)[\\w]+-$",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

This is my test case:

    Programs
    Dsfasdf. Programs Programs Dsfasdf. Dsfasdf. as is wow woah! woah. woah? okay. 
    he said, "hi." aasdfa. wsdfalsdjf. go-to go-
to
asdfasdf.. , : ; " ' ( ) ? ! - / \ @ # $ % & ^ ~ `  * [ ] { } + _ 123

Any help would be awesome

My expected result would be to match all the words ie.

Programs Dsfasdf Programs Programs Dsfasdf Dsfasdf
as is wow woah woah woah okay he said hi aasdfa
wsdfalsdjf go-to go-to asdfasdf 

the part I’m struggling with is matching the words that are split up between lines as one word.

ie.

go-
to
  • 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-06-06T13:02:54+00:00Added an answer on June 6, 2026 at 1:02 pm
    \p{L}+(?:-\n?\p{L}+)*
    \   /^\ /^\ /\   /^^^
     \ / | | | |  \ / |||
      |  | | | |   |  ||`- Previous can repeat 0 or more times (group of literal '-', optional new-line and one or more of any letter (upper/lower case))
      |  | | | |   |  |`-- End first non-capture group
      |  | | | |   |  `--- Match one or more of previous (any letter, upper/lower case)
      |  | | | |   `------ Match any letter (upper/lower case)
      |  | | | `---------- Match a single new-line (optional because of `?`)
      |  | | `------------ Literal '-'
      |  | `-------------- Start first non-capture group
      |  `---------------- Match one or more of previous (any letter between A-Z (upper/lower case))
      `------------------- Match any letter (upper/lower case)
    

    Is this OK?

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

Sidebar

Related Questions

I'm trying to write a very simple regular expression that matches any file name
I am trying to write a regular expression that matches only any of the
I'm trying to write a regular expression for Java's String.matches(regex) method to match a
I'm trying to write a regular expression that matches the (non-javadoc) comments in the
In java, I'm trying to write a regular expression that will match a unit
I'm trying to write a regular expression that matches strings like these: title(Hello+World) #=>
Trying to write a regular expression for java that will return true ONLY if
I am trying to write a regular expression for ASP.NET MapPageRoute that matches a
I am trying to write a regular expression that will detect a word that
I'm trying to write a regular expression for html code that looks like this:

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.