Home > Net >  How can i match this expression, and remove the last word?
How can i match this expression, and remove the last word?

Time:02-04

Update: XAUUSD LONG
100pip classic
Locked riskfree
/\bUpdate\b.*\b(short|long)\b/gi

Gives me:

100pip classic
Locked riskfree

I want:

Update: XAUUSD
100pip classic
Locked riskfree

Basically want to remove the word long or short if Update: XAUUSD exists.

CodePudding user response:

You can switch the capture groups capturing the first part instead. In the replacement use capture group 1.

Using a case insensitive match:

\b(Update\b.*)\b(?:short|long)\b

See a regex101 demo.

CodePudding user response:

If I understand your question correctly, the first line is the only one that matters. You also didn't mention what language you're using - is it just in a text editor?

You can replace this pattern (case insensitive) to an empty string:

(?<=update:\s*xauusd) (short|long)\b

It uses a positive lookbehind.

  • Related