1

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.

3
  • 3
    As the regex tag info states, all questions with this tag should also include a tag specifying the applicable programming language or tool. Commented Mar 1, 2016 at 21:34
  • the character class [89] does not need the alternation |. Commented Mar 1, 2016 at 21:36
  • Depending on the language you are using, you could use a group reference, such as \1 or \2. Or just use two regex: one for 7.8 and one for 7.9 Commented Mar 1, 2016 at 21:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you need to capture the whitespace?
If the whitespace must be kept in the output, I need to capture the whitespace.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.