March 28th, 2009, 02:21 AM
These are the possible strings:
I'm trying to write a regex to capture the following values from each string:
Every regex I write only captures these 1 character backreferences:
I can't figure out the regex that will capture the values above from the strings above.
The closest I got was this:
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.
March 28th, 2009, 04:36 AM
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.
March 28th, 2009, 05:18 AM
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).
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.
Comments on this post
March 28th, 2009, 04:13 PM
So how do you know what the ID is? What if you had a file named abc_123_b_c_789.jpg?
Originally Posted by threequestions
Oh god, I can't believe I missed that My fault for trying to be helpful after 2am.
 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.
Last edited by requinix; March 28th, 2009 at 04:21 PM.
March 29th, 2009, 12:38 AM
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.