2

I will just give a few examples as I think that is the best way to describe what I want, but basically I want to only keep the number but prepend text to the start depending on the first number (1 or 2: AAA, 3: BBB, 4 or 5: XYZ):

INITIAL STRINGS:

Blah 12345 Blah
Blah 22345 Blah
Blah 32345 Blah
Blah 42345 Blah
Blah 52345 Blah

RESULT:

AAA12345
AAA22345
BBB32345
XYZ42345
XYZ52345

Regex to search for:

([0=9])([0-9]{4})

Regex To replace with:

(SOME WAY OF DECIDING BETWEEN AAA|BBB|XYZ depending on \1!)\1\2

Is this possible?

FrostbiteXIII
  • 901
  • 9
  • 22
  • I've had a look around but everything I found always replaced the text with something in the search string, and wondered if this was possible. Thanks in advance folks. :) – FrostbiteXIII Sep 26 '16 at 09:49
  • 1
    Bit of advice folks - all great solutions (upvoted all). Like Nadia's as it was the right answer and she was first; Wiktor's as it was right and went into more detail; and Casimir's as it was right and works without the need for Notepad++ (which I really like). Normally when everyone is right I give the "answer" to the person was the lowest cred as it's tough starting out. Does that sound fair? Thanks for all the help! – FrostbiteXIII Sep 26 '16 at 10:25
  • I agree. Also, since revo removed his answer, I upvoted two of his other answers, so we are all happy I hope. – Wiktor Stribiżew Sep 26 '16 at 12:02

3 Answers3

5

You don't need to use the regex conditional feature here (i.e.: (?(condition)true|false).

You can do it creating a fake last line (without newline at the end) with the data you need:

Blah 12345 Blah
Blah 22345 Blah
Blah 32345 Blah
Blah 42345 Blah
Blah 52345 Blah
AAA#BBB#XYZ

and use this pattern:

^[^0-9\n]*(?:((?|[12](?=(?>.*\n)*([^#]+))|3(?=(?>.*\n)*[^#]*#([^#]+))|[45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+)))\d+).*|.*\z)

and this replacement:

$2$1

demo

pattern details:

^ # start of a line
[^0-9\n]* # all that isn't a digit
(?:
    ( # first capture group for the number
        (?| # branch reset: all capture have the same number inside (2 here)
            [12]
            (?= # lookahead to reach the good string
                (?>.*\n)* # reach the last line
                ([^#]+)   # capture the first string
            )
          |
            3
            (?=
                (?>.*\n)*
                [^#]*#    # skip the first string
                ([^#]+)   # capture the second
            )
          | # same thing
            [45](?=(?>.*\n)*(?:[^#]*#){2}([^#]+))
        ) # close the branch reset group
        \d+
     ) # close the capture group 1
     .* # match the end of the line
  |
    .*\z # match the last line
)
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • 1
    I deleted my answer since it wasn't the only answer with unique solution and going to upvote yours since it's *different* and has an *idea*. – revo Sep 26 '16 at 10:20
  • Wow, Casimir, this completely messed with my head - will look more closely at this when I have more time, but very interesting thank you! :) – FrostbiteXIII Sep 26 '16 at 10:21
  • 1
    @revo: thanks, I think your solution was the good one, I didn't know it was possible to use a conditional replacement in notepad++. – Casimir et Hippolyte Sep 26 '16 at 16:57
3

There seems to indeed be a way to do that with Notepad++ as it supports conditional replacement, which is detailed in the accepted answer of this question: How to use conditionals when replacing in Notepad++ via regex

But please note that it might be easier and quicker to just do multiple Find/Replace for each of your cases...

Community
  • 1
  • 1
Nadia Cerezo
  • 602
  • 5
  • 12
3

Use

^.* ((?:([12])|(3)|([45]))\d{4}) .*

and replace with

(?2AAA:(?3BBB:XYZ))$1

The pattern matches:

  • ^ - start of line
  • .* - zero or more chars other than a newline up to the last
  • ((?:([12])|(3)|([45]))\d{4}) - a space, 1 specific digit (1 or 2 - Group 2, 3 - Group 3, 4 - Group 4, and 4 more digits
  • .* - the rest of the line

Replacement pattern details:

  • (?2 - If the second group matched,
    • AAA - add AAA
    • : - or
    • (?3 - if Group 3 matches,
      • BBB - inset BBB
      • : - or
      • XYZ - insert XYZ
    • )
  • )
  • $1 - and insert Group 1 contents (the whole 5 digit number).

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563