Regexp-matching character n times in the line.

R

Rodrigo A B Freire

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:

(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

The regexp should match every email address containing 4 underscores.

Any idea?

Thank you,

Rod.
 
P

Paul Lalli

Rodrigo A B Freire said:
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:

(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

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
 
B

Brian McCauley

Rodrigo said:
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:

(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

The regexp should match every email address containing 4 underscores.

/^([^_]*_){4}[^_]*$/
 
R

Rodrigo A B Freire

Brian,

Worked like a charm! ;]

Thank you,
Rod.

Brian McCauley said:
Rodrigo said:
I'm trying to build a regular expression that matches a exact count of
a character.
I explain.
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

The regexp should match every email address containing 4 underscores.

/^([^_]*_){4}[^_]*$/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top