-2

I'm breaking my head about following problem. I need to match dot in the word under certain condition.

Example:

D.A.R.Y.L. or se.ven or go/the    will be untreated.
Dr. Odissey. or look, or look:    will be treated. 

I was using before:

([A-Z]+[.\\-/]+)+([A-Z]+[.\\-/]?)*

It was partially working, but it wouldn't distinguish for Dr. Odissey. and so on.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Nadja
  • 19
  • 4
  • 2
    I'm not sure what you mean by 'untreated' and 'treated'. Could you clarify? – Jerry May 31 '13 at 19:56
  • How would you handle the following: `the qui.ck brown fox` or `jumped over the d.og?` or `Hey, Lady! You call him Dr.!` – Ro Yo Mi May 31 '13 at 20:36

4 Answers4

1

Try this pattern in Java style:

(?<!\\S)[A-Za-z]+\\p{Punct}(?!\\S)

you can replace \\p{Punct} with the punctuation characters of your choice [.,:;]

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0

If you are looking for words optionally ending with punctuation you can use

\b[a-zA-Z]+\p{P}?(?=\s|$)

\b is a non word boundary

\p{P} matches any punctuation character

(?=\s|$) would check for a space or end of string

Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

It seems (?) that you want to match/replace a dot if (and only if) it's followed by whitespace or the end of the string. Is that correct?

In that case, assuming you want to change dots into exlamation marks,

newString = oldString.replaceAll("(?<=^|\\s)(\\w+)\\.(?=\\s|$)", "$1!");

will change

D.A.R.Y.L. or se.ven or go/the Dr. Odissey. or look, or look:

into

D.A.R.Y.L. or se.ven or go/the Dr! Odissey! or look, or look:
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
0

If you're only looking to find the dots inside a word then you can use this

\b[.]\b

Regular expression image

Edit live on Debuggex

Or if you're only looking for dots at the end of words, then try this

\b[.](?:\s|$)|(?:^|\s)[.]\b

Regular expression image

Edit live on Debuggex

Of course this will match the . after D.A.R.Y.L.

Community
  • 1
  • 1
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43