Does anyone now of a regular expression that will omit words in a string while replacing others?

Recommended Answers

You mean negative lookahead of regex? You could, it will search for the first occurrence. What would you do next if you find it?

OK, if I were to do it, I would firstly replace all occurrences of the word I specified with a unique set of symbols. Then …

Jump to Post

All 4 Replies

What do you mean by "words"??? If you are talking about A-Za-z, then you may use [^A-Za-z]... It means "not" A-Za-z in the reqex. Remember that the "^" must be inside []. If it is outside, it means "start with"...

Actual specific words

eg omit the "and"s in the string but replace everything else with a specific word.

I've heard about negative lookaround,can that help?

You mean negative lookahead of regex? You could, it will search for the first occurrence. What would you do next if you find it?

OK, if I were to do it, I would firstly replace all occurrences of the word I specified with a unique set of symbols. Then I replace all other words I don't care for. Afterward, I replace back the word I place in with unique set of symbols.

The problem I am seeing is how your text is like? Could you give me an example? Just want to be sure before I throw in an idea... I'm having some thought in a wrong way right now...

Thanks for the suggestion, I figured that inverse replace is too difficult to implement with regular expressions so I did it using a similar method to the one you described above. The only difference is that I didn't change the specified word to symbols but merely checked if the other words in the string matched the specified word, if they didn't then I would replace it.

I only wanted to use regular expressions because I thought it would be simpler (less coding).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.