Conditional appending regex syntax

Cheetah
2014-06-02
2014-06-02
  • Cheetah
    Cheetah
    2014-06-02

    Hello! I'm having trouble defining proper regex syntax to make mass conditional appends. Actually I'm a total noob when it comes to regex so please bear with me as I learn.

    Example occurrences to edit (across multiple files)
    irrelevant454
    blahblah 12365
    blahblah 548
    irrelevant 54545100
    blahblah 987569412397421
    blahblah 1
    blahblah1254
    irrelevant210 421

    The "blahblah" part is always at the start of the lines I wish to edit, then sometimes some space(s) but sometimes not, and then a random number of varying length.

    What I want to do is simply append a "0" to each number of each line that starts with "blahblah". Output would be;
    irrelevant454
    blahblah 123650
    blahblah 5480
    irrelevant 54545100
    blahblah 9875694123974210
    blahblah 10
    blahblah12540
    irrelevant210 421

    Hopefully somebody can save me from having to manually edit those lines.

     
    Last edit: Cheetah 2014-06-02
  • dail8859
    dail8859
    2014-06-02

    Use the regular expression (^blahblah *\d+) and replace it with \10

    Now some explanation. ^blahblah is the beginning of the line and the part you want to match, the space and asterisk matches 0 or more spaces, \d+ matches 1 or more digits. The entire thing is wrapped in parenthesis to mark everything it matches as group 1. When replacing, \1 uses the text from group 1, and 0 is the zero you want to append at the end.

    Note: Be careful when running this. If you run it more than once it will append a zero each time.

     
  • Cheetah
    Cheetah
    2014-06-02

    Worked like a charm.

    Thank you for your fast response and explanation. This has saved me from hours of tedium.