Please help me in the following pattern

Current text


Intended status after first round of find & replace in Notepad ++


As seen above, in the first round - I intend to search a specific pattern * ALPHA* BI_209* and replace with *BI_209*BI_209*.
Please help me with the correct regex in FIND as as well as REPLACE fields to achieve what I plan to get. Note there a varying texts like ';111,' ..... ';222,' .... ';AAA,' in between which shouldn't get changed or affected.

If I get a pattern for find & replace, it would be help in a second (and further) round(s) of multiple find & replace - to achieve this output

  1. Find what: ALPHA(.*'BI_209')

    Replace with: BI_209\1

    Explanation: what is matched by the regex between the parentheses gets saved, and can be recalled with \1. Parentheses can define multiple capture groups e.g. (ALPHA)(.*)('BI_209'), that can be reused with backreferences \1, \2, \3 ...

  2. Putting this all together, you could do all substitutions in one pass:

    Find what: ALPHA(.*)'(BI_209|BI_213|BI_220)'

    Replace with: \2\1'\2'

    Here, the | symbol specifies alternative strings to search for.

  3. Here's a more general solution in case you later add different codes:

    Find what: ALPHA(.*)'(BI_\d{3})'

    Replace with: \2\1'\2'

    Here \d matches any digit, and is a synonym for [0-9]; {3} is the repetition factor; \d{3} matches any sequence of exactly three digits.

Note: since paretheses are special characters, if you want to specify a literal ( or ) you need to escape it with a backslash, like so: \( and \).

  • I went step by step in order to better explain the available options. Máté Juhász's answer immediately solves your last point and is more general. – simlev Mar 9 '17 at 10:49
  • Awesome explanation. I used the logic you gave to do everything at one shot . i.e search for (ALPHA)(.*)(BI_)(.*)(') and replace by \3\4\2\3\4\5 – user705628 Mar 9 '17 at 11:07
  • Great, I like the fact that you could use the information provided to elaborate your own solution. – simlev Mar 9 '17 at 11:16
  • Simlev - Couple of questions – user705628 Mar 10 '17 at 8:46
  • Simlev - Couple of questions. 1) - How to split when a bracket comes in way of text ..... example -> ALPHA('Hello') . I tried to use (ALPHA(')(.*)(')) . The plan was to slit like this ALPHA(' ->1 Hello ->2 ' ->3 .. it showed error. Is the only way to find the back references like this (ALPHA)(.*)(')(.*)(') meaning it splits ALPHA ->1 ( -> 2 ' -> 3 Hello ->4 ' ->5 – user705628 Mar 10 '17 at 8:54

To find a text ALPHA...'BI_xxx' and replace to 'BI_xxx'...'BI_xxx':

  • open find and replace dialog
  • check "regular expression"
  • find what: ALPHA(.*'(BI_\d+)')
  • replace to: \2\1
    test it here

This will match any 'BI_xxx' where xxx is a number of at least one digit.

  • 1
    Worth my upvote, I didn't think of using nested capture groups. The inner set of () should be moved inside the '' in order to provide the exact solution. – simlev Mar 10 '17 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.