0

Suppose I have string coming from Input with name (for eg: Mr . Aditya Jha). How do I remove salutation from the start of input?

List of salutations that can come are:

Mr, Mrs, Dr, Miss, Ms, Rev, Mr. , Mr. , Dr. , Miss. , Ms. , Rev. , Mr . , Mr . , Dr . , Miss . , Ms . , Rev .

Any solution or regex statement which can consider all of these salutations?

I tried this:

name.replaceAll("\\s{2,}", " ").replaceFirst("(?i)(Mr . )", "").replaceFirst("(?i)(Mr |Mr. )", "").trim()

It is working, but for name like amra khan, it is removing mr.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • I have to use replace two times--First to remove the third possibility, then to remove the first two. Is there any better approach ? – Aditya Jha IN Apr 07 '20 at 11:09
  • Yes there are a solution, just use `Mr\s*\.?\\s* ` some people here close and down-vote question and answer for that I removed my correct answer – YCF_L Apr 07 '20 at 11:18
  • 1
    Does it mean you only need to replace it at the start of the string? Try `.replaceFirst("(?i)^\\s*Mr\\b[\\s.]*", "")`. If anywhere inside the string, use ``.replaceFirst("(?i)\\bMr\\b[\\s.]*", "")``. – Wiktor Stribiżew Apr 07 '20 at 11:33
  • 1
    If you need an answer, please explain what you need. – Wiktor Stribiżew Apr 08 '20 at 22:09
  • I tried this : name.replaceAll("\\s{2,}", " ").replaceFirst("(?i)(Mr . )", "").replaceFirst("(?i)(Mr |Mr. )", "").trim(); It is working, but for name like amra khan, it is removing mr – Aditya Jha IN Apr 10 '20 at 13:00
  • Also, this is only one salutation. I have a exhaustive list of 20 saluations (like Mrs. , Dr. , Rev. etc) that has also to be removed if they come with the name. – Aditya Jha IN Apr 10 '20 at 13:08
  • So, please add all the relevant details to the question. Also, when you provide feedback, add `@` and user name for the user to get notified of this feedback. – Wiktor Stribiżew Apr 10 '20 at 15:34
  • 1
    So, this is still not clear if these salutations come at the start of string, or anywhere in the string. Try `"(?i)\\b(M(?:iss|rs?|s)|Dr|Rev)\\b[\\s.]*"` with `replaceAll`, see [this regex demo](https://regex101.com/r/AWkbvZ/1). – Wiktor Stribiżew Apr 10 '20 at 15:36
  • @WiktorStribiżew updated the question with relevant details – Aditya Jha IN Apr 10 '20 at 16:02
  • But please revert your attempt. Please see the comments, please provide feedback. I believe [my last suggestion](https://stackoverflow.com/questions/61078462/how-to-remove-all-type-of-prefix-salutation-from-string-in-java?noredirect=1#comment108168907_61078462) covers all your cases. – Wiktor Stribiżew Apr 10 '20 at 16:03
  • @WiktorStribiżew It worked for me. I tried this: replaceFirst("(?i)^(Rev|Prof|Ms|Miss|Smt|Sri|Shri|Shree|Mrs|Mr|Dr|Jr|Sr)(\\s\\.|\\.|)\\s", "" ) – Aditya Jha IN Apr 10 '20 at 16:16

1 Answers1

1

You may use

name = name.replaceAll("\\s{2,}", " ").replaceFirst("(?i)^\\s*(?:M(?:iss|rs?|s)|Dr|Rev)\\b[\\s.]*", "").trim();

See the regex demo

Pattern details

  • (?i) - case ignoring option
  • ^ - start of string
  • \s* - 0+ whitespaces
  • (?:M(?:iss|rs?|s)|Dr|Rev) - M followed with iss, r, rs, s, or Dr or Rev (you may add more after | here)
  • \b - word boundary
  • [\s.]* - 0 or more whitespaces or dots.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397