homepage Welcome to WebmasterWorld Guest from 54.205.168.88
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
matching hash pound sign in regular expression
mrealty




msg:4316922
 3:17 pm on May 24, 2011 (gmt 0)

Never though it would be this hard to do a simple match even after searching google.

I'm trying to do this:
$string = "sdf #123456 something something else test testing"

I want to look for the occurrence of pound sign somewhere in a string, immediately followed by exactly 6 numbers, immediately followed by a white space. I want the digit in between the pound and the white space in some variable $num.

Every time I make an attempt at the regex, it keeps commenting out the # in the IDE (using a program called Perl Express). I assume I'm not doing it right. Here's my messed up attempt:

$string =~ m/\#(\d{6})\s/;
$num = $1;

What am I doing wrong here? Thanks in advance.

 

janharders




msg:4316938
 3:29 pm on May 24, 2011 (gmt 0)

You're doing it right, the IDE is wrong. It's common for syntax highlighting to fail at perl regular expressions.
the same will usually happen when you say something like
$string =~ m/"test/
and it'll think that you have a runaway multiline quote.

\# is not needed, # is not a special character in regexps.

but the code should work -- have you tried just to run it?

mrealty




msg:4317023
 6:10 pm on May 24, 2011 (gmt 0)

"have you tried just to run it?"

Uh..short answer? No. :-) Sorry, have this old habit of doing pseudo code in English in notepad, then translate that English/logic to the language (PERL in this case) straight into the IDE. When the statement I am trying to make is kind of long, I just try to piece it together in the IDE (it's incomplete so I can't run the code yet)...and when the IDE balks, I usually take it as Gospel.

Can't believe I got something right for a change. Thanks janharders.

rocknbil




msg:4317527
 5:35 pm on May 25, 2011 (gmt 0)

Hmm, got it to work but can't get the first condition - pattern at end of string - to work. Lost interest. :-)

#!/usr/bin/perl
print "hash match test (or hash mat? :-) )\n";
@strings = (
'This is a pound # sign ',
'This is also a pound with six numbers #12345',
' And #87654322 another but too long',
' one more #987654 with spaces'
);
foreach $string (@strings) {
if ($string =~ /#(\d{6})[^\d]+/) {
print "\"$string\" matches on $1\n";
}
else { print "\"$string\" does not match.\n"; }
}

brotherhood of LAN




msg:4317533
 5:49 pm on May 25, 2011 (gmt 0)

rocknbil, the 2nd item in your array has 5 numbers not 6, looks like you intended to match 2 & 4.

mrealty




msg:4317552
 6:35 pm on May 25, 2011 (gmt 0)

Hi there: It was solved in the opening post. The IDE made it appear as though there was a syntax error, but there wasn't. It appears rocknbil changed the search criteria from the original post. As brotherhoodofLAN said, your script above did what was expected of it. I've gotten my script running and in production. Thanks for the help.

P.S. My old script was 1,115 lines of code without using regex (string functions and positions). Rewritten to use regex whenever and wherever possible it's now 55 lines of code. Sure helps to brush up on regex, that's for sure.

rocknbil




msg:4318024
 4:24 pm on May 26, 2011 (gmt 0)

2nd item in your array has 5 numbers not 6, looks like you intended to match 2 & 4.


<facepalm> Learn to count first, then pattern match . . .

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved