3

I'm not using any particular coding language, simply a program with "find" and "replace" where both fields use Regex. For example, the phrase

too many professionals

would turn into

tooo many professsionals

I want to "find" any occurrences of double letters, which I know I can do with

(.)\1

What confuses me is the "take the doubled letter and triple it" bit. Is there any symbol in Regex that refers to "the current character" or something like that?

EDIT: Thank you all for your suggestions. It turns out that the "replace" field does not, in fact, use Regex, which IMO is bullcrap, and my question is now worthless.

Not a real meerkat
  • 5,604
  • 1
  • 24
  • 55

4 Answers4

3

Your current pattern (.)\1 which uses a backreference, is the on the right track. Try this find and replace:

Find:    (.)\1
Replace: $1$1$1

Demo

This approach matches any single letter followed by the same letter, and then replaces that pair by three of the same letter.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
3

Tim's expression is incorrect, because (.) refers to any char, which we don't wish to replace that. We only wish to replace (English) letters or [A-Za-z].

The correct way to do so might be an expression with an i flag:

([a-z])\1

or maybe an expression without i flag:

([A-Za-z])\1

Demo

Test

const regex = /([a-z])\1/gmi;
const str = `too many   professionals
would    turn into

tooo many professsionals`;
const subst = `$1$1$1`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
2

Double to triple

Find (?m)(?:([a-zA-Z])(?!\1)|^)(([a-zA-Z])\3)(?!\3)
Replace $1$2$3

https://regex101.com/r/tOpl82/1

Explained

 (?m)                          # Multi-line mode     
 (?:
      ( [a-zA-Z] )                  # (1), Character behind
      (?! \1 )                      #      not a dup.
   |                              # or, 
      ^                             # BOL
 )
 (                             # (2 start)
      ( [a-zA-Z] )                  # (3), Character to dup check
      \3                            #      this is duped.
 )                             # (2 end)
 (?! \3 )                      # The dup can't be a triple
  • 1
    Only answer, that takes into account that already triple letters probably shouldn't get "quadled" and won't get matched =) – bobble bubble Jun 03 '19 at 00:56
0

It's almost part of your question :).

It also depends on the regex flavor that you are using.

E.g. for gnu sed, it's

$ echo 'ffoobarr' | sed -e 's/\(.\)\1/\1\1\1/g'
fffooobarrr

So, to tripple it, I just used \1\1\1 as a replacement pattern.

Emma
  • 27,428
  • 11
  • 44
  • 69
Michi
  • 681
  • 1
  • 7
  • 25