User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » perl regex question Page [1]  
raiden
All American
10505 Posts
user info
edit post

so I'm doing some playing around, trying to learn up some perl, and I have a question.

what if I had two values, lets say "dog" and "dogs". If I'm looking just for "dog", how can I get it to not match "dogs".

something like this.



if ( $hash{animal} =~ /^dog/ ) {
if ( $hash{hair} =~ /^longhair/i ) {
print "singluar longhaired dog";
}
else {
print "fail, you are stupid \n";
}
}
if ( $hash{animal} =~ /^dogs/i ) {
if ( $hash{hair} =~ /^short/i ) {
print "bunch of short haired dogs. \n";
}
else {
print "fail, you are stupid \n";
}
}
else {
print "\nFAIL";
}

7/24/2011 1:58:43 PM

BigMan157
no u
103354 Posts
user info
edit post

/^dog$/

7/24/2011 2:05:57 PM

qntmfred
retired
40726 Posts
user info
edit post

/^dog[^s]/

[^s] means exclude s

[Edited on July 24, 2011 at 2:08 PM. Reason : .]

7/24/2011 2:08:06 PM

raiden
All American
10505 Posts
user info
edit post

excellent, both worked perfectly, but I'm going to use the $ since I have a few more of these to do and don't want to get dependent on the exclude box.

many thanks you 2.

7/24/2011 2:21:02 PM

lewisje
All American
9196 Posts
user info
edit post

/^fag(got)?[s]?$/

7/25/2011 8:24:42 AM

scrager
All American
9481 Posts
user info
edit post

don't forget that /^dog$/ matches a string that is exactly 'dog'. It will not match in "I have a dog".

Also, /dog[^s]/ will not match 'dogs', but it will match 'doggies'. You might want to try word boundaries instead: /\bdog\b/

you might want to look in to 'look ahead' with regular expressions as well, if the library you are using supports them.

a good website for reference is http://www.regular-expressions.info

[Edited on July 25, 2011 at 3:50 PM. Reason : .]

7/25/2011 3:49:20 PM

Specter
All American
6575 Posts
user info
edit post

ibtxkcd

7/25/2011 4:16:03 PM

lewisje
All American
9196 Posts
user info
edit post

/puss(y|ies)/

7/25/2011 8:41:13 PM

 Message Boards » Tech Talk » perl regex question Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.39 - our disclaimer.