9 Replies - 2447 Views - Last Post: 21 June 2012 - 09:30 AM

#1 MartinKravec  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 20-June 12

Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 06:57 AM

Is this correct regular expression ? [PERL]

I believe it is, but the program thinks something else..
Variable must be "data.conf"

if($var !~ /^[a-zA-Z]+[a-zA-Z0-9]*[\.]{1}[a-zA-Z]+$/){ die("Wrong param"); }

Is This A Good Question/Topic? 0
  • +

Replies To: Is this correct regular expression ? [PERL]

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 07:27 AM

If my regex fu is working (fat chance!) this expression would match a line

starting with one or more alphabetic characters
followed by zero or more alphanumeric characters
followed by a single literal '.'
followed by one or more alphabetic characters

This should match "data.conf".
Was This Post Helpful? 2
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 07:35 AM

View PostMartinKravec, on 21 June 2012 - 01:57 PM, said:

Is this correct regular expression ? [PERL]

I believe it is, but the program thinks something else..

You think the computer is mistaken? :)

One thing that I'd look into more closely is this bit: [\.]{1}. If I am not mistaken, the dot character is not a special-char inside a character class, so the backslash isn't needed. You also don't really need the class, seeing as it's only one char. The {1} is also quite uneccessary seeing as the default will be to match exactly one instance of the char.

So I'd try:
/^[a-zA-Z]+[a-zA-Z0-9]*\.[a-zA-Z]+$/


Was This Post Helpful? 2
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 07:45 AM

@Atli - you're right that the backslash isn't needed, however, it's allowable. Likewise the {1} - redundant, but not wrong.

As long as we're talking about not-wrong, if you're using a named character class, use the name.

\w matches alphanumeric characters, so go ahead and use that instead of [a-zA-Z0-9]
Was This Post Helpful? 1
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 07:51 AM

I know those things are true in PHP and Javascript, but I haven't work with Perl much so I though I'd mention it.

Doesn't \w also match the _ character though?
Was This Post Helpful? 0
  • +
  • -

#6 MartinKravec  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 20-June 12

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 07:58 AM

View PostAtli, on 21 June 2012 - 07:51 AM, said:

I know those things are true in PHP and Javascript, but I haven't work with Perl much so I though I'd mention it.

Doesn't \w also match the _ character though?


\w is only [a-zA-Z0-9]. I am just looking to perl book
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 08:09 AM

View PostAtli, on 21 June 2012 - 09:51 AM, said:

I know those things are true in PHP and Javascript, but I haven't work with Perl much so I though I'd mention it.


No, your suggestions were good ones. In regex, simpler is almost always better. My only point was that the original expression was not wrong - not that yours weren't more correct.

Quote

Doesn't \w also match the _ character though?


Not in perl - it does in Java, which makes sense since then it matches the characters allowable in a variable name in non-initial position.
Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3710
  • View blog
  • Posts: 5,958
  • Joined: 08-June 10

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 09:22 AM

Quote

Not in perl - it does in Java, which makes sense since then it matches the characters allowable in a variable name in non-initial position.

OK, I see. Are you sure though? I did a simple test on my Ubuntu box an it did seem to match the underscore.
#!/usr/bin/perl

$var = "UPPERlower1337_";
if($var =~ /^\w+$/){
        print "Match!\n";
}
else {
        print "No match.\n"
}


Result: "Match!"


I also tested the original regexp in this thread and I can't find a problem with it.
#!/usr/bin/perl

$var = "data.conf";
if($var !~ /^[a-zA-Z]+[a-zA-Z0-9]*[\.]{1}[a-zA-Z]+$/){
        die("Invalid file name!");
}
else {
        print "Everything is fine.\n"
}


I get "Everything is fine" until I mess up the file name, at which point it dies like I would expect.

By the way, I also tested the expression to incorporate all the mentioned improvements, and that works fine too.
/^[a-zA-Z]+\w*\.[a-zA-Z]+$/

This post has been edited by Atli: 21 June 2012 - 09:23 AM

Was This Post Helpful? 2
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 09:26 AM

I'll be damned, it does match.
Don't oughta, according to the Camel, but maybe they've changed it in recent years. (my copy of the Camel is venerable)
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Is this correct regular expression ? [PERL]

Posted 21 June 2012 - 09:30 AM

Moved to Perl. Please post in the correct forum in the future.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1