Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
#!/usr/bin/perl

$mystring = "[2004/04/13] The date of this article.";
if($mystring =~ m/(\d+)/) {
        print "The first number is $1.";
}

Perl returns 2004, but Kiki and Kodos return /04/. Why that?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Since you did not provide your Python code (or whatever Kiki and Kodos are) then it can be hard to say for sure but my guess is that the pattern syntax for Kiki and Kodos differs from Perl in that you do not need the forward-slash delimiters. For example the equivalent in Python would be:

import re
m = re.search('(\d+)', "[2004/04/13] The date of this article.")
print m.group(0)

Notice that the pattern does not include forward-slashes.

share|improve this answer
1  
You're absolutely right. Thank you. (kiki and kodos are RegEx Debugger GUIs). –  chris May 12 '12 at 22:41
add comment

In Perl it's important to realize that the regex operator isn't m, it's the double slashes - /..../ (where .... is actually your regular expression). The m is optional if you use /.

The following are all equivalent:

$mystring =~ m/(\d+)/;

$mystring =~ /(\d+)/;

$var = '(\d+)';
$mystring =~ /$var/;
share|improve this answer
    
Technically it is the m// that is the operator, but the m is optional in some circumstances. –  Mat May 13 '12 at 13:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.