Welcome to WebmasterWorld Guest from 54.204.106.194

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

matching hash pound sign in regular expression

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

New User

5+ Year Member

joined:May 13, 2009
posts: 21
votes: 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.
3:29 pm on May 24, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 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?
6:10 pm on May 24, 2011 (gmt 0)

New User

5+ Year Member

joined:May 13, 2009
posts: 21
votes: 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.
5:35 pm on May 25, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 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"; }
}
5:49 pm on May 25, 2011 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4847
votes: 6


rocknbil, the 2nd item in your array has 5 numbers not 6, looks like you intended to match 2 & 4.
6:35 pm on May 25, 2011 (gmt 0)

New User

5+ Year Member

joined:May 13, 2009
posts: 21
votes: 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.
4:24 pm on May 26, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 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 . . .
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members