
mhx
Enthusiast
Aug 5, 2002, 5:12 AM
Post #9 of 11
(3575 views)
|
|
Re: [mauto] how to extract characters between 2 pipes
[In reply to]
|
Can't Post
|
|
Can you explain the above regex in words, as i am a little confused? Sure. The part in red captures the whole string before pqr:
$count = ($str =~ /(.*)pqr/)[0] =~ tr/|//; That alone is a very basic regex. The .* matches zero or more arbitrary characters. The parentheses around .* capture the string that .* matched.
$count = ($str =~ /(.*)pqr/)[0] =~ tr/|//; Putting parentheses around the regex makes the regex operator work in list context, returning a list of all matched substrings. The index operator [0] simply selects the first (and only) element of that list, which is the string holding everything before pqr.
$count = ($str =~ /(.*)pqr/)[0] =~ tr/|//; Now, this string is used with the transliteration operator tr, which is normally used to replace certain characters. If no replacement characters are given, the operator simply counts the characters (in our case pipes) and returns the total count, which is then stored in $count. You can have a look at [url=http://www.perldoc.com/perl5.8.0/pod/perlop.html#Regexp-Quote-Like-Operators]perldoc perlop for details. Hope this helps. -- mhx
At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."-- Frodo
|