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 8779761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:52:44+00:00 2026-06-13T19:52:44+00:00

I am trying to slash out pattern as specified using regex , but in

  • 0

I am trying to slash out pattern as specified using regex , but in replacement also replaces wanted character . specifying boundary does not help in this case .

 String name = "Dr.Dre" ;  
     Pattern p = Pattern.compile("(Mr.|MR.|Dr.|mr.|DR.|dr.|ms.|Ms.|MS.|Miss.|Mrs.|mrs.|miss.|MR|mr|Mr|Dr|DR|dr|ms|Ms|MS|miss|Miss|Mrs|mrs)"+"\\b");
     Matcher m = p.matcher(name);
     StringBuffer sb = new StringBuffer();
     String namef = m.replaceAll("");    
     System.out.println(namef);

Input : Dr.Dre or Dr Dre or Dr. Dre

> output(expected) : Dre or Dre or Dre

Edit:

Thanks for help , but there is little regex issue I am facing:
Program:

String name = "Dr. Dre" ;  
Pattern p = Pattern.compile("(Mr\\.|MR\\.|Dr\\.|mr\\.|DR\\.|dr\\.|ms\\.|Ms\\.|MS\\.|Miss\\.|Mrs\\.|mrs\\.|miss\\.|MR|mr|Mr|Dr|DR|dr|ms|Ms|MS|miss|Miss|Mrs|mrs)"+"\\b");
Matcher m = p.matcher(name);
String namef = m.replaceAll(""); 
System.out.println(namef);

For above program I receive output as:
. Dre
while the desired output is :
Dre

  • 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-13T19:52:45+00:00Added an answer on June 13, 2026 at 7:52 pm

    Dot in a regular expression means “any character”. You need to escape it with a backslash, which in turn needs to be escaped in a string literal:

    Pattern p = Pattern.compile("Mr\\.|MR\\.|Dr\\.|mr\\.|DR\\.|dr\\.|ms\\."); // etc
    

    Note that you’ll end up with a double space after removing “Dr.” from “or Dr. Dre” though…

    EDIT: For some reason (I haven’t worked out why), a space after a dot doesn’t count as a word boundary. If you change your pattern to use \\s instead of \\b, so replace a single whitespace character, it works for “Dr. Dre” – but as noted in comments, it then fails for “Dr.Dre”. You could either remove the word boundary entirely and add a space to the later parts of the pattern (“DR |Dr |” etc) or use (\\s|\\b) which works for the cases I tried it on, but may well have other undesirable side-effects.

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

Sidebar

Related Questions

I'm trying to add a trailing slash to urls using PHP. It can't be
I am really trying to do my best but I can't really find out
I'm trying to use a back slash in console.log() and within <p></p> but it
I am trying to run this query but can't figure out why MySQL doesnt
I am trying to use MSBuild community tasks to remove the slash from the
I'm trying to create a splash screen using LWUIT. I want a form to
I'm trying to use this to create a splash screen, but I only get
I'm trying to set an image as the background using this code: root.setStyle(-fx-background-image: url('splash.jpg');
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
Trying to figure out how I can do this properly. The print_r looks like

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.