Friday, January 11, 2013

RegEx groups and variables

I was trying to move the end of a bit of mysql code to the middle and there were quite a few rows and I didn't want to to it manually. I know PHP fairly well and could have used it to accomplish my goal but that would have required parsing everything through a browser and that causes problems. I've started to learn RegEx and found that I can use it to accomplish my goal even better.

I want to make a note here to remind myself of what I did. I made a group () with matching rules in it, then another, then another. The first group matched the part of the line of text which I wanted to go before the part I wanted to move. The second match group was the part I wanted to move to the end and the third group was the part I wanted to put in the middle. e.g. (some RegEx here)(Some more RegEx)(Last bit)

That all went into a search field. The replace field looked like this $1$3$2. Each variable (which I didn't even have to set) representing a group. It was very nice.

This is a site that I found very helpful in this process
http://gskinner.com/RegExr/

No comments:

Post a Comment