How to retain characters preceding a match?
-
I'm using an application called Panorama X (see provue.com) which has a function, RegExReplace, which, in a block of text, replaces every occurrence of a string defined by a regular expression with a different string. For example, RegExReplace(TextBlock,"^[;]","//") will search a block of text called TextBlock for all lines beginning with a semi-colon and will replace the semi-colon with two slashes. In some lines, the ";" is preceded by a number of spaces but I still want to change it. The only way I've been able to do this is to copy the characters before the ";" and do this: RegExReplace(TextBlock,"^[ ]+[;]", +"//") which is a little clumsy. Is there a better way to do it? I'm very new to RegEx. michael
-
I'm using an application called Panorama X (see provue.com) which has a function, RegExReplace, which, in a block of text, replaces every occurrence of a string defined by a regular expression with a different string. For example, RegExReplace(TextBlock,"^[;]","//") will search a block of text called TextBlock for all lines beginning with a semi-colon and will replace the semi-colon with two slashes. In some lines, the ";" is preceded by a number of spaces but I still want to change it. The only way I've been able to do this is to copy the characters before the ";" and do this: RegExReplace(TextBlock,"^[ ]+[;]", +"//") which is a little clumsy. Is there a better way to do it? I'm very new to RegEx. michael
Look in the documentation of your regex engine for "capture groups". You can use a capture group to grab any leading spaces, then reference it in your replacement string. That bit will probably be called "backreference". If you are doing anything serious with regex, I can heartily recommend Expresso[^] Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
-
Look in the documentation of your regex engine for "capture groups". You can use a capture group to grab any leading spaces, then reference it in your replacement string. That bit will probably be called "backreference". If you are doing anything serious with regex, I can heartily recommend Expresso[^] Cheers, Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
I'll check out capture groups - thank you. And thanks for the thought on Expresso but I'm on a Mac. michael