Regex: Simple replaceAll of words

T

Tony

Hi, I've had this problem for about a week now, I am trying to replace
the occurrences of given words in a documents for example if I was
given the text:

"7.00pm-8.00pm. Jack uncovers a secret government prison and Sherry
scolds Palmer for destroying everything they've worked for by going
public. At the police station, Sam is unimpressed when Kim tries to
convince him that she's really the victim of a kidnapping. Drama,
starring Kiefer Sutherland and Leslie Hope."

I have a list of words including "they've", "over", "or" etc...

I want to replace all occurrences of this using a regular expression,
using:

(they've)|(over)|(or) etc it will replace all occurrences, even those
in the middle of a word like 'worked' would become 'wked'.

However using ([\\W]((they've)|(over)|(or))[\\W])+ to only pick ones
with whitespace on either side, on their own; it will miss out the
ones at the start of the document because they don't have a space
infront, or the following one if you had say "or they've", "they've"
would still be there!

If anyone has any advice or solutions I'd be very thankful.

Tony Pierson
 
A

Alan Moore

Also, the '+' at the end isn't doing anything.


Try:

(\\b((they've)|(over)|(or))\\b)+


\b is for word boundary

Hi, I've had this problem for about a week now, I am trying to replace
the occurrences of given words in a documents for example if I was
given the text:

"7.00pm-8.00pm. Jack uncovers a secret government prison and Sherry
scolds Palmer for destroying everything they've worked for by going
public. At the police station, Sam is unimpressed when Kim tries to
convince him that she's really the victim of a kidnapping. Drama,
starring Kiefer Sutherland and Leslie Hope."

I have a list of words including "they've", "over", "or" etc...

I want to replace all occurrences of this using a regular expression,
using:

(they've)|(over)|(or) etc it will replace all occurrences, even those
in the middle of a word like 'worked' would become 'wked'.

However using ([\\W]((they've)|(over)|(or))[\\W])+ to only pick ones
with whitespace on either side, on their own; it will miss out the
ones at the start of the document because they don't have a space
infront, or the following one if you had say "or they've", "they've"
would still be there!

If anyone has any advice or solutions I'd be very thankful.

Tony Pierson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top