Wednesday, June 26, 2013

Regex prepend and append

I was working on making all the countries below as options in a select dropdown box. For the upcoming example, you can use Sublime or Notepad++.

The countries have the following form (country_value {tab_character} country_name):

Sample Input:
AD Andorra AE United Arab Emirates AF Afghanistan AG Antigua and Barbuda AI Anguilla AL Albania AM Armenia
An option html element has the following form:



There are two things we need to do:

  1. trim all the whitespaces
  2. Replace with the option tag


1. Trim all the whitespaces

You can trim all the whitespaces by matching the following:
Pattern: ^[ \t]+|[ \t]+$
Replace with: (nothing)

2. Replace with the option tag

Match them into two blocks, you can use \1 to reference first block (first matching parenthesis below),
Pattern: (.*)\t(.*)
Replace with: 

No comments:

Post a Comment