help needed with regular expression please

Soldato
Joined
18 Oct 2002
Posts
9,804
Location
Southampton, Hampshire
being a n00b when it comes to reqular expressions I could do with some help when it comes to conditional string replacement/deletion.

i'm trying to match a string with a word in the middle that could vary. if there is a match then i'd like to remove the end bit after the varying word.

i.e.
msg.setEventType(EventType.OPEN_DOOR.toString()
the text in green is the varying string. i can match it using:
Code:
(setEventType\()(EventType\.).*(toString\(\))

the text in orange is what i'd like to remove.

however my problem comes when it comes to removing the toString() bit - I'm not sure how to do it so that I also keep the text in green.

can anyone help please?

thanks in advance! :)
 
Last edited:
I mainly use regular expressions in Perl, so this may vary a little between languages.

When you cluster (I think that's the right term) using brackets each part gets put into a special variable, $x, where x is the number of that part e.g $1, $2 etc. You can use these either after the regexp or inside the regexp.

Try this and see if it works:

s/(setEventType\()(EventType\.).*(toString\(\))/$1$2/;

In Perl s/ is the search and replace mechanism. This could vary between different implementations of regular expressions.

Jim
 
Back
Top Bottom