Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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. ;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading