Regex 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 Languages - MoreRegex 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 March 5th, 2013, 11:54 PM
warmgrey14 warmgrey14 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 1 warmgrey14 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 59 sec
Reputation Power: 0
Other - Three python regex expressions around underscores

in python regex,

a) in the first expression i have to find all characters before the first underscore in patterns like this:

cannon_mac_23567_prsln_333
jones_james_343342_prsln_333
smith_john_223462_prsln_333

so, i have to get cannon, jones, and smith

b) in a separate expression i have to find all characters between the first and second underscore. so, i need to find mac, james, and john in the examples above.

c) in the last expression i have to find the first underscore

Reply With Quote
  #2  
Old March 6th, 2013, 12:32 AM
requinix's Avatar
requinix requinix is online now
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,701 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 5 h 23 m 54 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
What have you tried so far?

Reply With Quote
  #3  
Old March 6th, 2013, 12:23 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 507 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 12 m 26 sec
Reputation Power: 385
This looks like a school assignment. Please do not expect us to to your homework for you. It would probably take me less than 5 minutes, but I wouldn't be doing you a favor.

Please come up with what you have tried so far and tell us if something is not working properly, then we can help you solving problems you encounter.

Reply With Quote
  #4  
Old March 6th, 2013, 01:33 PM
warmgrey9 warmgrey9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 warmgrey9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by requinix
What have you tried so far?



i know very little about regular expressions and trying to help my wife with some file renaming for her work using a renaming application that supports regular expressions. to do what needs to be done has to be done in three passes, which is why i listed the a), b), c).

to try and get the cannon, jones, and smith from part a) i tried ^[^_]+(?=_) and a few others lifted from forum posts but they didn't work so i figured i better reach out to people that actually know what they're doing.

p.s. i'm posting under a different user name because my login didn't work and i did not get the password reset email

Reply With Quote
  #5  
Old March 6th, 2013, 01:36 PM
warmgrey9 warmgrey9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 warmgrey9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by Laurent_R
This looks like a school assignment. Please do not expect us to to your homework for you. It would probably take me less than 5 minutes, but I wouldn't be doing you a favor.

Please come up with what you have tried so far and tell us if something is not working properly, then we can help you solving problems you encounter.


hi there, i'm actually trying to help my wife with something for her work - she's using a renaming application that supports regular expressions. to do what needs to be done has to be done in three passes, which is why i listed the a), b), c).

if you can help that would be great. i tried a few things lifted from other forums but they didn't work.

thanks...

Reply With Quote
  #6  
Old March 6th, 2013, 01:56 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 507 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 12 m 26 sec
Reputation Power: 385
OK, for the first one.

There are various possibilities, but here is one: you need to match start of string, a number of characters other than _, followed by _, with a capture of the characters other than _.

This could be:

Code:
/^([^_]+)_/


The second one could be something like this:

Code:
/^[^_]+_([^_]+)_/


(I haven't tested them, as I am working in Perl rather than PHP, but I think they should work.)

I leave the third one to your tries for the time being. Try those and tell us if they do what you want.

Reply With Quote
  #7  
Old March 6th, 2013, 02:22 PM
warmgrey9 warmgrey9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 warmgrey9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by Laurent_R
OK, for the first one.

There are various possibilities, but here is one: you need to match start of string, a number of characters other than _, followed by _, with a capture of the characters other than _.

This could be:

Code:
/^([^_]+)_/


The second one could be something like this:

Code:
/^[^_]+_([^_]+)_/


(I haven't tested them, as I am working in Perl rather than PHP, but I think they should work.)

I leave the third one to your tries for the time being. Try those and tell us if they do what you want.


hi, no result. the renaming app uses Python syntax for regular expressions. i don't know how that compares to Perl syntax.

screen capture here: http://www.bigidearesults.com/files/regex/screen-1.jpg

i was getting a result for the first one using this but it was not working right. see this screen capture for test showing problem with first line

http://www.bigidearesults.com/files/regex/screen-2.jpg

the objective with part one and two is to find and swap the first and last name using this app

Reply With Quote
  #8  
Old March 6th, 2013, 03:24 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 507 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 12 m 26 sec
Reputation Power: 385
I do not know what the application you are using is, but it seems that if you are using this, you need to remove the beginning and ending "/" (which mark beginning and end of regex in Perl) from the regex I gave you, i.e., try with:

Code:
^([^_]+)_


and

Code:
^[^_]+_([^_]+)_


Last time I used Python was I think in 2002, I certainly don't remember the syntax details (and they may have changed in between). But most dynamic languages of today, including I think Python, use a regex syntax directly derived from Perl, so what I have given (at least the pure regex) you should work with the necessary adaptation to Python in terms of what you put around your regex. After all, the most common regex package used il most of these languages is called PCRE, for Perl Compatible Regular Expression.

Just to show how the regex I provided works, the following is a test of it under the Perl debugger:

Code:
  DB<1> $c = "cannon_mac_23567_prsln_333";

  DB<2> print $1 if $c =~ /^([^_]+)_/;
cannon
  DB<3> $c = "jones_james_343342_prsln_333";

  DB<4> print $1 if $c =~ /^([^_]+)_/;
jones

  DB<5> print $1 if $c =~ /^[^_]+_([^_]+)_/;
james

Last edited by Laurent_R : March 6th, 2013 at 03:26 PM.

Reply With Quote
  #9  
Old March 6th, 2013, 04:30 PM
warmgrey9 warmgrey9 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 4 warmgrey9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 58 sec
Reputation Power: 0
Quote:
Originally Posted by Laurent_R
I do not know what the application you are using is, but it seems that if you are using this, you need to remove the beginning and ending "/" (which mark beginning and end of regex in Perl) from the regex I gave you, i.e., try with:

Code:
^([^_]+)_


and

Code:
^[^_]+_([^_]+)_


Last time I used Python was I think in 2002, I certainly don't remember the syntax details (and they may have changed in between). But most dynamic languages of today, including I think Python, use a regex syntax directly derived from Perl, so what I have given (at least the pure regex) you should work with the necessary adaptation to Python in terms of what you put around your regex. After all, the most common regex package used il most of these languages is called PCRE, for Perl Compatible Regular Expression.

Just to show how the regex I provided works, the following is a test of it under the Perl debugger:

Code:
  DB<1> $c = "cannon_mac_23567_prsln_333";

  DB<2> print $1 if $c =~ /^([^_]+)_/;
cannon
  DB<3> $c = "jones_james_343342_prsln_333";

  DB<4> print $1 if $c =~ /^([^_]+)_/;
jones

  DB<5> print $1 if $c =~ /^[^_]+_([^_]+)_/;
james


it must be a bug in the renaming application i'm using. thanks for your help

Reply With Quote
  #10  
Old March 7th, 2013, 03:44 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 507 Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level)Laurent_R User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 4 Days 19 h 12 m 26 sec
Reputation Power: 385
Hi,

You could try to do some prints at various points in the program, to see what is done correctly and what is not right.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > Other - Three python regex expressions around underscores

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