How can I find repeated letters with a Perl regex?

后端 未结 11 1253
孤独总比滥情好
孤独总比滥情好 2020-12-09 03:18

I am looking for a regex that will find repeating letters. So any letter twice or more, for example:

booooooot or abbott

I won\'t know the

相关标签:
11条回答
  • 2020-12-09 03:56

    You might want to take care as to what is considered to be a letter, and this depends on your locale. Using ISO Latin-1 will allow accented Western language characters to be matched as letters. In the following program, the default locale doesn't recognise é, and thus créé fails to match. Uncomment the locale setting code, and then it begins to match.

    Also note that \w includes digits and the underscore character along with all the letters. To get just the letters, you need to take the complement of the non-alphanum, digits and underscore characters. This leaves only letters.

    That might be easier to understand by framing it as the question:

    "What regular expression matches any digit except 3?"
    The answer is:
    /[^\D3]/

    #! /usr/local/bin/perl
    
    use strict;
    use warnings;
    
    # uncomment the following three lines:
    # use locale;
    # use POSIX;
    # setlocale(LC_CTYPE, 'fr_FR.ISO8859-1');
    
    while (<DATA>) {
        chomp;
        if (/([^\W_0-9])\1+/) {
            print "$_: dup [$1]\n";
        }
        else {
            print "$_: nope\n";
        }
    }
    
    __DATA__
    100
    food
    créé
    a::b
    
    0 讨论(0)
  • 2020-12-09 03:57

    FYI, aside from RegExBuddy, a real handy free site for testing regular expressions is RegExr at gskinner.com. Handles ([[:alpha:]])(\1+) nicely.

    0 讨论(0)
  • 2020-12-09 04:00

    How about:

    (\w)\1+
    

    The first part makes an unnamed group around a character, then the back-reference looks for that same character.

    0 讨论(0)
  • 2020-12-09 04:02

    I think this should also work:

    ((\w)(?=\2))+\2

    0 讨论(0)
  • 2020-12-09 04:04

    Use \N to refer to previous groups:

    /(\w)\1+/g
    
    0 讨论(0)
提交回复
热议问题