Donald Alexandre
Donald Alexandre

Reputation: 37

Regex : adding a number before specific number pattern found

I'm trying to add a number 9 before a specific pattern in a file containing many EANs, like this example :

7,82897E+11 50 MATCHS DE HOCKEY 39,95   23,97   40  1   0   0
7,82924E+11 EINSTEIN    34,95   20,97   40  1   0   0
15  BEAUX LIVRES & SCIENCE  94,85   56,91   40  3   0   0
7,82101E+11 SCIENCE  COMME VOUS NE L'AVEZ   34,95   20,97   40  1   0   0

I'm specifically searching for EANs (which are at the begining of data line) who starts with either 7,8 or 7,9 and I must add a 9 before those numbers.

So the pattern will look like 9,78xxx or 9,79xxx after the replacement.

I used this regexp to find those strings:

\t\t[7][,][8|9]

The two \t\t serves me to not replace the number AFTER the first one in the line.

I was thinking of this : \t\t[9],[7][8|9] but the last part [8|9] is not working, as I expected... I dont know how to just place the number found (8 or 9)...

Hope this is possible to do!

Thanks for any help! Greatly appreciated.

Upvotes: 1

Views: 70

Answers (2)

frank
frank

Reputation: 1247

Your search pattern could be

/^([\t]*)([7][,])([8|9])/

the replacement could be:

$19,7$3

this will modify your sample from

7,82897E+11 50 MATCHS DE HOCKEY 39,95 23,97

to:

9,782897E+11 50 MATCHS DE HOCKEY 39,95 23,97

What it does:

a) the search:

it splits the search into three groupsmarked by the (..)

1) the beginning of the line

2) does the string starts with 7,

3) is it followed either by 8 or 9

the in the subtitution it adds the string 9,7 and by refering to the third group using $3it adds either 8 or 9. The rest of the string stays untouched.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You can use the following regex based replacement:

^([ \t]*)7,([89])

And replace with ${1}9,7$2 (or just $19,7$2 if it is JavaScript, \g<1>9,7\2 in Python, \19,7\2 in POSIX (because POSIX BRE only supports up to 9 backreferences), or \019,7\2 in some weird regex flavors).

Instead of [ \t], you can use \s shorthand character class if it is supported.

See the regex demo

Also please note that [8|9] matches 1 symbol: 8, | or 9. The pipe character loses its special meaning inside a character class.

The regex breakdown:

  • ^ - start of string
  • ([ \t]*) - (Group 1) zero or more spaces or tabs
  • 7, - a sequence of characters 7,
  • ([89]) - (Group 2) either 8 or 9.

If you need to match the beginning of the lines, you can either use a multiline flag, or inline version of it, or a special flag.

Upvotes: 1

Related Questions