0

I am having trouble figuring out how I could get this regex replace to work. I am using textmate and trying to get this:

title="cuba1" (could be any number range from 1-50 and any country name)

to be replaced with this:

data-group="cuba1, cuba" class="cuba"

I have multiple countries with multiple numbers that may or may not go on a country (cuba1, cuba2, etc) as I am setting these on areas and sometimes I need to have groups of areas. This would allow me to implement them faster without needing to do this by hand.

Any help would be appreciated.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Justin Lonas
  • 189
  • 1
  • 11

1 Answers1

4

Regex:

title="(([a-zA-Z]+)\d*)"

Replacement:

data-group="$1, $2" class="$2"

Tested in Notepad++. These constructs are pretty standard though; should work with any regex implementation.

EDIT: Comes with the usual caveat: regular expressions are not a good fit for HTML

hoipolloi
  • 7,984
  • 2
  • 27
  • 28
  • this will capture both the word one and word with digits? – Justin Lonas Nov 22 '12 at 23:28
  • 1
    @Justin: it will now! I only tested with number before. It now works with and without number. However, without number produces data-group="cuba, cuba" class="cuba" which is probably not desirable. – hoipolloi Nov 22 '12 at 23:32
  • Actually no, the first and second found items are the same for some reason. `title="newzealand1"` With the above regex gets me: `data-group="newzealand1, newzealand1" class="newzealand1"` instead of `data-group="newzealand1, newzealand" class="newzealand"` – Justin Lonas Nov 22 '12 at 23:44
  • Try incrementing the group numbers i.e \1, \2 = \2, \3. Sometimes the whole regex is treated as group 1. – hoipolloi Nov 22 '12 at 23:48
  • 1
    The problem here is that \w matches numbers and the \d is optional. Use this regex instead: title="(([a-zA-Z]+)\d*)" – Francis Gagnon Nov 22 '12 at 23:53
  • Thanks Francis. That was it :) – Justin Lonas Nov 22 '12 at 23:55
  • `Regex: title="(([a-zA-Z]+)\d*)"` `Replacement: data-group="$1, $2" class="$2"` – Justin Lonas Nov 22 '12 at 23:57
  • @Francis - of course! I've cleaned up the post to incorporate all feedback. – hoipolloi Nov 23 '12 at 00:01