Regular expression questions

Discussion in 'Perl Misc' started by Hung Truong, May 8, 2004.

  1. Hung Truong

    Hung Truong Guest

    Hi,

    I'm new at this regular expression. How can I do a match for strings that
    1) contain three (or more) pairs of double letters
    2) have an even number of 'a's

    Thanks,
    Hung
     
    Hung Truong, May 8, 2004
    #1
    1. Advertising

  2. Hung Truong

    Ben Morrow Guest

    Quoth (Hung Truong):
    > Hi,
    >
    > I'm new at this regular expression. How can I do a match for strings that
    > 1) contain three (or more) pairs of double letters


    /(?: (.)\1 .* ){3}/x

    If you actually mean 'letter' (i.e., a string like '{{' shouldn't match)
    then you want

    /(?: ([[:alpha:]]) \1 .* ){3}/x

    > 2) have an even number of 'a's


    /^ [^a]* (?: a [^a]* a [^a]* )* $/x

    If you want to do both at once you're best off combining them with Perl
    logic, rather than trying to write one regex.

    Ben

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." - C.S.Lewis
     
    Ben Morrow, May 8, 2004
    #2
    1. Advertising

  3. Hung Truong

    Bob Walton Guest

    Hung Truong wrote:

    ....


    > I'm new at this regular expression. How can I do a match for strings that
    > 1) contain three (or more) pairs of double letters
    > 2) have an even number of 'a's
    >


    ....

    > Hung
    >


    Homework time, huh? You can find what you're looking for in:

    perldoc perlre

    in the section titled "Regular Expressions". Just type that line at
    your operating system's command prompt and read up.
    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, May 8, 2004
    #3
  4. Hung Truong

    Jay Tilton Guest

    (Hung Truong) wrote:

    : I'm new at this regular expression. How can I do a match for strings that
    : 1) contain three (or more) pairs of double letters

    Depending on whether 'xxx' should be considered to have one set of doubled
    letters or two, you could say:

    print "has three or more pairs of letters" if 3<=(()=/(.)\1/g);

    or:

    print "has three or more pairs of letters" if 3<=(()=/(.)(?=\1)/g);

    : 2) have an even number of 'a's

    print "has an even count of the letter 'a'" unless (()=/a/g)%2;
     
    Jay Tilton, May 8, 2004
    #4
  5. Hung Truong

    Ala Qumsieh Guest

    Purl Gurl wrote:

    > if ($input =~ s/([$letter])/\1/g % 2 == 0)


    First of all, you don't need a character class for a single letter.
    Second of all, there is no need for needless substitutions when you can
    use tr/// to count the occurrence of characters.

    The only gotcha is that tr/// does not interpolate its searchlist so
    you'll need to eval it as a double-quoted string instead.

    --Ala
     
    Ala Qumsieh, May 8, 2004
    #5
  6. Hung Truong

    Matt Garrish Guest

    "Purl Gurl" <> wrote in message
    news:...
    >
    > After personal debate
    >


    It's bad enough when a person starts hearing voices, but arguing with them
    is a sure sign you should check yourself into the local psychiatric ward.
    Explains a lot about you, though...

    Matt
     
    Matt Garrish, May 8, 2004
    #6
  7. Hung Truong

    Ala Qumsieh Guest

    Purl Gurl wrote:
    > After personal debate, I have decided a character [ ] class
    > could be removed for the originating author's unclear parameters,
    > which leave a reader guessing at intent.
    >
    > Your point is taken, graciously.


    Thank you.

    > However, transliteration would be more of a challenge
    > to use with a need to write transliteration code which
    > interpolates a variable.


    You only have to wrap it up inside an eval:

    $count = eval qq("$str" =~ tr/$letter//);

    > Nonetheless, the orginating author, who has yet to respond
    > to articles, should retain my deprecated \1 usage simply
    > to annoy.


    True. But this question smells a lot like homework. I would rather point
    the OP to the relevant docs than supply a solution.

    --Ala
     
    Ala Qumsieh, May 8, 2004
    #7
  8. Hung Truong

    Anno Siegel Guest

    Abigail <> wrote in comp.lang.perl.misc:
    > Hung Truong () wrote on MMMCMIII September MCMXCIII in
    > <URL:news:>:


    [...]

    > :) 2) have an even number of 'a's
    >
    >
    > I wouldn't use a regex for that.
    >
    > print "Even number of 'a's\n" unless y/a/a/c;


    Hmm?

    Scalar y/a/a/c counts the number of non-a's, so that identifies strings
    of a's only.

    Anno
     
    Anno Siegel, May 9, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,382
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    883
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    12,048
    vforvikash
    Dec 29, 2008
  4. Jeremy
    Replies:
    1
    Views:
    334
    Reinhold Birkenfeld
    Jul 11, 2005
  5. bbxrider

    newbie regular expression questions

    bbxrider, Nov 22, 2003, in forum: Perl Misc
    Replies:
    11
    Views:
    165
    Gunnar Hjalmarsson
    Nov 24, 2003
Loading...

Share This Page