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 28th, 2009, 02:21 AM
threequestions threequestions is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 35 threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 11 m
Reputation Power: 15
JavaScript: Trying to Capture a Backreference

I'm trying to capture a value within some strings in JavaScript.

These are the possible strings:

http://www.url.com/sw/standard_b.jpg
http://www.url.com/sw/2_1238144910_3655223_ih_ee2.jpg
http://www.url.com/sw/2_1238144910_3655223_c_yr8.jpg
http://www.url.com/sw/56_1238193910_320_kh.jpg

I'm trying to write a regex to capture the following values from each string:

b
ih
c
kh

Every regex I write only captures these 1 character backreferences:

b
c

I can't figure out the regex that will capture the values above from the strings above.

The closest I got was this:
Code:
/(.+)([a|b|c|ih|kh])(.*)?(\.jpg)/i


It captures the value of "b," for example, in all the strings, but the moment I try to capture the value of "ih" or "ah" in the possible string sequences, it simply cuts off the second character and only returns "i" (from ih) or "k" (from kh).

I'm really stumped. The major problems occurs when the value to capture is longer than 1 character (as in ih|kh opposed to a|b|c).

All insight or solutions will be appreciated.

Reply With Quote
  #2  
Old March 28th, 2009, 04:36 AM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,717 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 7 h 29 m 55 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
How did you decide that it should capture b/ih/c/kh? It looked like you were getting the first letters after a _ but your expression doesn't reflect this at all.

Oh, and post all your JavaScript code. I'm thinking the problem may be there instead.

Reply With Quote
  #3  
Old March 28th, 2009, 05:18 AM
threequestions threequestions is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 35 threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 11 m
Reputation Power: 15
Those one- and two-character values are file ids in the name. My code is minimal as I'm just trying to alert() the actual value and nothing more, so there is nothing else interfering. I haven't decided to continue with the script, until I know how to do this. All I need is the value (file id).

Because refreshing the page all the time just to see an alert eventually got timely, I tested out the strings and the regex at http://www.regular-expressions.info/javascriptexample.html and clicked on the "show match" button, as it was much quicker to see the results, and it performed the same function as refreshing over and over again--and returned the same results. The problem is the pattern.

You should note that the first string's file name is a word followed by only one underscore while the others are just sequences of numbers separated by up to four underscores, and the file id is not always directly preceding the file extension. The lack of a uniform naming convention reflects the pattern I wrote, which does return good results so long as the file id is only one character long.

But the moment I try to return ih or kh, it cuts off the second character in the backreference.

Reply With Quote
  #4  
Old March 28th, 2009, 04:13 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,717 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 7 h 29 m 55 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
Quote:
Originally Posted by threequestions
You should note that the first string's file name is a word followed by only one underscore while the others are just sequences of numbers separated by up to four underscores, and the file id is not always directly preceding the file extension.

So how do you know what the ID is? What if you had a file named abc_123_b_c_789.jpg?


...
Oh god, I can't believe I missed that My fault for trying to be helpful after 2am.
Code:
([a|b|c|ih|kh])

[] marks a character set. It counts individual characters. So that set will look for any character a,b,c,h,i,k,| (because | is a regular character in there).

Don't want that? Don't use it.
Code:
(a|b|c|ih|kh)

Last edited by requinix : March 28th, 2009 at 04:21 PM.

Reply With Quote
  #5  
Old March 29th, 2009, 12:38 AM
threequestions threequestions is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 35 threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level)threequestions User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 18 h 11 m
Reputation Power: 15
Thank you very much, requinix. I didn't realize the square brackets did that. I thought placing the pipe | in between the brackets would denote OR.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreRegex Programming > JavaScript: Trying to Capture a Backreference

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