Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression exclude double character

Tags:

regex

I need to have regular expression which find a character which not following by same character after it. Mean exclude double even multiple same character.

For example, when i need to find 'e' character from string: "need only single characteeer", it is mean will find 'e' on each words breakdown as below:

"need" > not match because it has double 'e'

"only" > not match because no 'e'

"single" > match because has only single 'e'

"characteeer" > not match because has multiple of 'e'

Not sure whether it is possible or not. Any answer or comment will be highly appreciated. Thanks in advance.

UPDATE

Maybe my question above still ambiguous. Actually i need to find the 'e' character only instead the words. I am going to replace it with double character. So the one which already has double character will not replaced.

The main purpose is to replace 'e' with 'ee' for example. But the one which has 'ee' or 'eee' already, or even more 'e', will be untouched.

like image 578
Bayu Avatar asked Dec 23 '17 13:12

Bayu


2 Answers

UPDATE:

(?<!e)e(?!e)

Will match e not with negative lookbehind to prevent preceeding e and negative lookahead preventing following e.

Can be checked here

\b(([A-Za-z])(?!\2))+\b

Will match a word (sequence of one or more characters between A-Za-Z), with negative lookahead which prevents following character to be the same as last match, group 2, or using non capturing group.

/\b(?:([A-Za-z])(?!\1))+\b/g

however only will match because it doesn't contain repeated character. to match a word containing e but no ee

/(?<![a-z])(?=[a-z]*e)(?![a-z]*ee)[a-z]+/gi
like image 123
Nahuel Fouilleul Avatar answered Sep 23 '22 09:09

Nahuel Fouilleul


/\b([a-df-z]*e[a-df-z]*)\b\s*/g

You could add the flag case insensitive /i if needed.

Explanation:

/               : regex delimiter
  \b            : word boundary
  (             : start group 1
    [a-df-z]*   : 0 or more letter that is not "e"
    e           : 1 letter "e"
    [a-df-z]*   : 0 or more letter that is not "e"
  )             : end group 1
  \b            : word boundary
  \s*           : 0 or more spaces
/g              : regex delimiter, global flag

As you didn't give which language you're using, here is a perl script:

my $str = "need only single characteeer";
my @list = $str =~  /\b([a-df-z]*e[a-df-z]*)\b\s*/g;
say Dumper\@list;

Output:

$VAR1 = [
          'single'
        ];

And a php script:

$str = "need only single characteeer";
preg_match_all("/\b([a-df-z]*e[a-df-z]*)\b\s*/", $str, $match);
print_r($match);

Output:

Array
(
    [0] => Array
        (
            [1] => single 
        )

    [1] => Array
        (
            [1] => single
        )

)
like image 30
Toto Avatar answered Sep 22 '22 09:09

Toto