Home > Enterprise >  regex to match more than one occurrence of ; between any two random words Google sheets
regex to match more than one occurrence of ; between any two random words Google sheets

Time:01-17

I am trying to create a regex to match more than one occurrence of ; between two words (more than one letter).

Ex

MM ZZ ; ; ; ; NN GGG ; ; SSS. ;

I need to replace ; ; ; ; and ; ; with ; so I would have

MM ZZ;NN GGG;SS. ;

If my string looks like ZZ ; ; ; ; NN. ; I managed to get (?<=[a-zA-z])\b(.*)(?=[a-zA-z])\b which works

But if my string is more complicated like MM ZZ ; ; ; ; NN GGG ; ; SSS. ; then it does not work

How to do this?

CodePudding user response:

You might use:

(?<=[a-zA-Z])\s*;\s;[\s;]*(?=[a-zA-Z])

Explanation

  • (?<=[a-zA-Z]) Positive lookbehind, assert a char a-zA-Z to the left
  • \s*;\s; Match at least 2 times ; between optional whitespace chars
  • [\s;]* Match optional whitespace chars or ; chars
  • (?=[a-zA-Z]) Positive lookahead, assert a char a-zA-Z to the right

And replace with a single ;

Regex101 demo

const regex = /(?<=[a-zA-Z])\s*;\s;[\s;]*(?=[a-zA-Z])/g;
const s = `MM ZZ ; ; ; ; NN GGG ; ;  SSS. ;
ZZ ; ; ; ; NN. ;
MM ZZ ; ; ; ; NN GGG ; ;  SSS. ;`;
console.log(s.replace(regex, ';'))


If the single semicolons should be separated by at least a single whitespace char:

(?<=[a-zA-Z])\s (?:;\s ) (?=[a-zA-Z])

In this pattern, this part \s (?:;\s ) matches 1 whitespace chars, and repeats 1 times ; and 1 whitespace chars.

Regex101 demo

  • Related