₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,326,111 members, 8,425,013 topics. Date: Friday, 12 June 2026 at 12:18 AM

Toggle theme

Replace Options In Regular Expression - Computers - Nairaland

Nairaland ForumScience/TechnologyComputersReplace Options In Regular Expression (635 Views)

1 Reply

Replace Options In Regular Expression by CarlowOlson(op): 10:18am On Nov 02, 2012
Hello Members,

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: ([0-9]+[a-z])
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: ([0-9]+[a-z])
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
Carlow Olson
1 Reply

Replace Your Damaged Or Broken Laptop Screen Here At Cheap PriceHow To Enable Hibernate Mode Feature On Windows 8/8.1 Shutdown OptionsBroadband/dsl Options In Ogun State234

Technical Help With ModemWindows Don't Display My Actual RAM SizeHelp With Internet Plan