Search code examples
regexbackreferenceultraedit

Need regular expression to replace before and after while keeping the numbers in the middle


I have the following:

itemid=44'>Red Flower</a>

I need it to be this:

_ITEMID_START_44_ITEMID_END_

Can this be done with regular expressions? I need to keep the id (44 in the example), and replace everything on the left with _ITEMID_START_and everything on the right with _ITEMID_END_.

Note: The itemid is one digit or two but never no more than two.

I found something about tagged regular expressions and backreferences which seems like it would work but the syntax is killing me.

I tried this (and other attempts):

Find What: ^(\bitemid=\b)^([0-9][0-9]^)\b'>\b[a-z]+\b</a>\b)
Replace With: ^(\b_ITEMID_START_\b^2^(\b_ITEMID_END_\b

I am using UltraEdit to do the find and replace in over 20,000 *.html files. Any help would be very much appreciated.


Solution

  • The solution of Casimir et Hippolyte and also first solution of Avinash Raj work both in UltraEdit with selecting Perl as regular expression engine. The second search string of Avinash Raj requires removing backslash left of character ' in search string to work in UltraEdit.

    UltraEdit has 3 regular expression engines: UltraEdit, Unix and Perl.

    The search string in the question is a mixture of UltraEdit and Perl regular expression syntax and therefore does not work.

    With UltraEdit reguar expression engine:

    Find what: itemid=^([0-9]+^)*</a>
    Replace with: _ITEMID_START_^1_ITEMID_END_
    

    With Unix or Perl regular expression engine:

    Find what: itemid=([0-9]+).*</a>
    Replace with: _ITEMID_START_\1_ITEMID_END_
    

    More secure because non greedy, but only with Perl regex engine:

    Find what: itemid=(\d+).*?</a>
    Replace with: _ITEMID_START_\1_ITEMID_END_
    

    IDM published the power tips tagged expressions for UltraEdit regex engine and Perl regular expressions: Backreferences for Perl regex engine.