Regex to match say char 't' exactly once in a string and no more than once

Discussion in 'Perl Misc' started by Gancy, Feb 3, 2005.

  1. Gancy

    Gancy Guest

    Some times Regex's which are supposed to be simple eats head. I have
    come across one such, though looks very simple, i am frustrted to right
    the expressions for it.

    I want to match a specifice charecter in a string, say 't', exactly
    once anywhere in the string, but should not match 't' more than once.
    sounds simple is it not.
    Gancy, Feb 3, 2005
    #1
    1. Advertising

  2. Gancy

    Tore Aursand Guest

    Re: Regex to match say char 't' exactly once in a string and nomore than once

    Gancy wrote:
    > I want to match a specifice charecter in a string, say 't', exactly
    > once anywhere in the string, but should not match 't' more than once.
    > sounds simple is it not.


    What have you tried so far? Have you checked the Perl documentation,
    especially the FAQ? Hint:

    perldoc -q count


    --
    Tore Aursand <>
    "If you can't annoy somebody with what you write, I think there's
    little point in writing." (Kingsley Amis)
    Tore Aursand, Feb 3, 2005
    #2
    1. Advertising

  3. Gancy

    Anno Siegel Guest

    Re: Regex to match say char 't' exactly once in a string and nomore than once

    Tore Aursand <> wrote in comp.lang.perl.misc:
    > Gancy wrote:
    > > I want to match a specifice charecter in a string, say 't', exactly
    > > once anywhere in the string, but should not match 't' more than once.
    > > sounds simple is it not.

    >
    > What have you tried so far? Have you checked the Perl documentation,
    > especially the FAQ? Hint:
    >
    > perldoc -q count


    That discusses counting of substrings, but not matching if there is
    only one occurrence.

    Now, in general it *is* the better solution to count and see if the
    result is 1, especially if going for multi-character strings or even
    general subpatterns.

    For the problem of matching single occurrences of a *single character*,
    there is a simple regex solution:

    my $char = 't';
    my $re = /
    ^ # beginning of string
    [^$char]* # any number of characters different from $char
    $char # a single $char
    [^$char]* # any number of characters different from $char
    $ # end of string
    /x;

    Anno
    Anno Siegel, Feb 3, 2005
    #3
  4. Gancy <> wrote:

    > I want to match a specifice charecter in a string, say 't', exactly
    > once anywhere in the string, but should not match 't' more than once.



    /^[^t]*t[^t]*$/


    > sounds simple is it not.



    Looks pretty simple to me...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 3, 2005
    #4
  5. Re: Regex to match say char 't' exactly once in a string and nomore than once

    Gancy wrote:

    > I want to match a specifice charecter in a string, say 't', exactly
    > once anywhere in the string, but should not match 't' more than once.
    > sounds simple is it not.
    >


    Here is funny way to do it without using regexp.

    if (abs index($string, $char) == rindex($string, $char)) {
    ...
    }

    --

    Rasto Levrinc
    http://sourceforge.net/projects/rlocate/
    Rasto Levrinc, Feb 3, 2005
    #5
    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. keithb
    Replies:
    4
    Views:
    743
    Patrick.O.Ige
    Apr 8, 2006
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,045
    Ian Collins
    May 9, 2006
  3. Merlin
    Replies:
    3
    Views:
    124
    Jeff 'japhy' Pinyan
    Jan 31, 2004
  4. Steven D'Aprano
    Replies:
    0
    Views:
    90
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    82
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page