Regexp-matching character n times in the line.

Discussion in 'Perl Misc' started by Rodrigo A B Freire, Sep 22, 2004.

  1. Regexp gurus,

    I'm trying to build a regular expression that matches a exact count of
    a character.

    I explain.

    Analyzing my mail log, I noticed that *every* traffic with sender
    addresses containing four underscores are spam. Examples:





    The regexp should match every email address containing 4 underscores.

    Any idea?

    Thank you,

    Rod.
     
    Rodrigo A B Freire, Sep 22, 2004
    #1
    1. Advertising

  2. Rodrigo A B Freire

    Paul Lalli Guest

    "Rodrigo A B Freire" <> wrote in message
    news:...
    > Regexp gurus,
    >
    > I'm trying to build a regular expression that matches a exact count of
    > a character.
    >
    > I explain.
    >
    > Analyzing my mail log, I noticed that *every* traffic with sender
    > addresses containing four underscores are spam. Examples:
    >
    >
    >
    >
    >
    > The regexp should match every email address containing 4 underscores.
    >
    > Any idea?


    perldoc -q count
    There are a number of ways, with varying efficiency.
    If you want a count of a certain single character
    (X) within a string, you can use the "tr///"
    function like so:

    $string = "ThisXlineXhasXsomeXx'sXinXit";
    $count = ($string =~ tr/X//);
    print "There are $count X characters in the string";

    Paul Lalli
     
    Paul Lalli, Sep 22, 2004
    #2
    1. Advertising

  3. Rodrigo A B Freire wrote:

    > I'm trying to build a regular expression that matches a exact count of
    > a character.
    >
    > I explain.
    >
    > Analyzing my mail log, I noticed that *every* traffic with sender
    > addresses containing four underscores are spam. Examples:
    >
    >
    >
    >
    >
    > The regexp should match every email address containing 4 underscores.


    /^([^_]*_){4}[^_]*$/
     
    Brian McCauley, Sep 22, 2004
    #3
  4. Brian,

    Worked like a charm! ;]

    Thank you,
    Rod.

    Brian McCauley <> wrote in message news:<cisinv$h34$>...
    > Rodrigo A B Freire wrote:
    > > I'm trying to build a regular expression that matches a exact count of
    > > a character.
    > > I explain.
    > >
    > >
    > >
    > >
    > > The regexp should match every email address containing 4 underscores.

    >
    > /^([^_]*_){4}[^_]*$/
     
    Rodrigo A B Freire, Sep 23, 2004
    #4
    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. =?Utf-8?B?bWF2cmlja18xMDE=?=

    SetAuthCookie works some times and fails some times?

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 23, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    543
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 23, 2006
  2. =?Utf-8?B?bWF2cmlja18xMDE=?=

    Forms Authentication Fails some times and not some times???

    =?Utf-8?B?bWF2cmlja18xMDE=?=, Mar 28, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    507
    =?Utf-8?B?bWF2cmlja18xMDE=?=
    Mar 28, 2006
  3. Jean-Michel Pichavant

    regexp matching end of line or comma

    Jean-Michel Pichavant, Nov 25, 2010, in forum: Python
    Replies:
    1
    Views:
    311
    Saul Spatz
    Nov 25, 2010
  4. Joao Silva
    Replies:
    16
    Views:
    390
    7stud --
    Aug 21, 2009
  5. Bart Lateur
    Replies:
    1
    Views:
    81
    Bart Van der Donck
    Jan 2, 2007
Loading...

Share This Page