Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 21st, 2013, 03:48 AM
raagu raagu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 raagu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 29 sec
Reputation Power: 0
Regular expression in perl

Hi
I am trying to have some regular expressions execution in perl but its not working as I expect
For example, in the following snippet

my $str = '/8B362F655DF011F60A4A579B94434EA8/employee/21';
if ( $str =~m!/8B362F655DF011F60A4A579B94434EA8/employee!) {
print "We found Pete or Steve!\n";
}

The regular expression "8B362F655DF011F60A4A579B94434EA8/employee" matches even for the value "/8B362F655DF011F60A4A579B94434EA8/employee/21'"

I wanted a regular expression which does strict matching like for the regular expression "/8B362F655DF011F60A4A579B94434EA8/employee" it should match only "/8B362F655DF011F60A4A579B94434EA8/employee"
and for the regex ""/8B362F655DF011F60A4A579B94434EA8/employee/([^/]*)" it should only value "/8B362F655DF011F60A4A579B94434EA8/employee/<atleaseOneShoudComeWithoutSlash>"

What is the regular expressions should I use ?

Reply With Quote
  #2  
Old February 21st, 2013, 04:04 AM
Milap Milap is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 Milap User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 8 m 58 sec
Reputation Power: 0
The above code is working fine...

for a strict matching pattern you can match '/' (you did the same thing..!!) character. or if this character comes always on the first position then use '^' (for more strict matching) at the begging.

Reply With Quote
  #3  
Old February 21st, 2013, 05:43 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 49 m 47 sec
Reputation Power: 3885
The regular expression matches if its contents can be found anywhere in the string. In the snippet you posted, if you want the string to end after 'employee', you have to say so, using the $ character that matches the end of the string, i.e.:
Code:
if ( $str =~m!/8B362F655DF011F60A4A579B94434EA8/employee$!) {
print "We found Pete or Steve!\n";
}

However, you're not actually using a pattern there at all. What you're looking for there is basically string equality so using a regexp at all is overkill. It could be accomplished more easily using:
Code:
if ( $str eq '/8B362F655DF011F60A4A579B94434EA8/employee' ) {
print "We found Pete or Steve!\n";
}
Comments on this post
Laurent_R agrees!

Reply With Quote
  #4  
Old March 7th, 2013, 09:18 AM
raagu raagu is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 raagu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 29 sec
Reputation Power: 0
Thanks for the reply.
What regular expression should I use to match
/8B362F655DF011F60A4A579B94434EA8/employee/<HereSomeThingShouldComeIWthoutSlash>

Reply With Quote
  #5  
Old March 7th, 2013, 12:48 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 547 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 2 h 48 m 20 sec
Reputation Power: 406
As Ishnid said, you don't need reguar expression for what you want. Test for equality of the strings with the eq operator.

Or, if you want something else, please explain further.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Regular expression in perl

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap