Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching in perl 1

Status
Not open for further replies.

WebmasterA

Programmer
Mar 26, 2004
25
0
0
US
Hello,

I have a pretty simple pattern matching question.
I need to match a number in a string.
For example I need to see if the number 5 is in the following stringsL
5,12,121,17 - yes
12,121,17,5 - yes
12,121,5,17 - yes
12,121,15,17 - no
12,121,51,17 - no
12,121,151,17 - no

Thank you very much for your help.

 
that's very simple yes

while(<>){
if(/5/){
print "yes\n";
} else {
print "no\n";
}
}

that's probably not enough for you though, what are you trying to do?

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Thanks for reply Mike.

As it is shown in examples above I need to find out if number 5 is the in the string. But I need to make sure number 5 is the stand alone number and not part of any other number like 15, 51 or 151.
5,12,121,17 - yes there is 5
12,121,17,5 - yes there is 5
12,121,5,17 - yes there is 5
12,121,15,17 - no there is not stand alone 5
12,121,51,17 - no there is not stand alone 5
12,121,151,17 - no there is not stand alone 5

Sorry for confussion.

Thanks again.
 
How about:

/[,5,]|[,5$]|[^5,]/

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
have a look at the documentation, should make things a bit clearer

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
chessbot's example was on the right track, but the regex will not perform as intended. the [brackets] denote character classes. Given the regex above, perl will not be matching just the number 5, rather, anything with a comma or a 5 in it will do.

This regex should work:
Code:
/(?:^|,)5(?:$|,)/

--jim
 
Thanks for replies guys.

I am at home now and will test it tomorrow.

Mike I have looked at the docs before posting it, but I managed to make it work for every case, but not for all at the same time. :(
 
Coderifous:
Thanks. I am not a perl adept yet, but that would work in javascript, I believe.

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
chessbot, no sweat. "Learning IS the point", as my old professor friend would say.
Code:
<script language="javascript">
    if('6,6'.match(/[,5,]|[,5$]|[^5,]/))    
    alert('regexes are fun');
</script>
I was curious to see if javascript for some reason strayed from the regex norm. My mini-test above showed me that the behavior seemed consistent to what I would expect.

myth debunked.

--jim
 
Using MikeLacey's code with a twist:

while(<>){
if(/\b5\b/){
print "yes\n";
} else {
print "no\n";
}
}


Michael Libeson
 
Coderifous,

Thank you very much.
I just tried it and it works perferctly.

Thanks everyone else who responded.
 
That's neat mlibeson - clear.

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Well, it might not. I didn't test it.

What is "\b"?

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
\b the word anchor, matches a boundary between a word character and a non-word character

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Code:
could you please explain, How this regex works 

/(?:^|,)5(?:$|,)/
 
No :) I never understand those ? experssion thingies...

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
From the initial posting the expr /(?:^|,)5(?:$|,)/ should look more like /(^|,)5($|,)/ if you do not want to use word boundries.

The first part (^|,) is a pattern match where the "5" is either at the beginning of the string "^" or is preceded by a ",".

The last part ($|,) is a pattern match where the "5" is either at the end of a string "$" or is followed by a ",".

I hope this helps.


Michael Libeson
 
Now that /(^|,)5($|,)/ looks ok.

Mike

"Deliver me from that bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
For posterity:

The ?: tells perl that the grouping (formed by parens) is not for capturing (into $1,$2,etc). This in theory would make it more efficient since perl isn't doing the extra work of recording the group matches.

That being said, \b5\b is simpler and probably more efficient.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top