Replace Options in Regular Expression
11-09-2012, 08:36 AM
|Replace Options in Regular Expression
Replacing options in a regular expression can get complicated quickly; you need to isolate the option you want to replace before making any changes. Doing so otherwise will potentially break the rest of the matches. This is why it's a good idea to segregate the different sections of your regular expression using parentheses as it makes it easier to work with individual segments. Regular expressions are used by a variety of scripting languages and are a powerful way to parse data from one or many files.
1. Open the script whose regular expression you would like to edit. Navigate to it.
2. Save a copy of the regular expression before changing anything by copying and pasting it into another file. Now find the parts of the regular expression you wish to change. So for example, you have:
This part of the expression will match one or more numbers, followed by one letter. If you wanted to replace that part of the expression, simply make your changes within the parentheses, otherwise you will start affecting the rest of the expression.
3. Use the built-in numbered variables to test the matches of your new expression. For each set of parentheses you have, the result of the match is stored in a numbered variable. For example, your expression is:
The sequence in which that appears in your expression is the number it's stored in. If it's the first set of parentheses, then you can see the result by using: print $1; This will print the text that matched your regular expression. If it was the second set of parentheses, you would use "$2" and so on. Continue testing your expression until it does exactly what you need it to do.
Thanks and Regards
|Possibly Related Threads...|
|How to Replace the Boot on a Front Load Frigidaire Washer||cicididi||2||2,502||
Last Post: Alexandro8100
User(s) browsing this thread: 1 Guest(s)