0
#!/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?

2 Answers 2

1

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.

1
  • 1
    You're absolutely right. Thank you. (kiki and kodos are RegEx Debugger GUIs). Commented May 12, 2012 at 22:41
0

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/;
1
  • Technically it is the m// that is the operator, but the m is optional in some circumstances. Commented May 13, 2012 at 13:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.