Help with regular expressions
Hey, I'm coding a c++ program using boost::regex with match_perl (pcre) enabled. Suppose I have a word from 4 to 12 chars in length with no newlines. I'd like to see, if it matches a particular regular expression, but I'm confused about something. Is there a way to tell, if a character is present at a certain location specified outside the regex?
Example: I have the word "linuxquestions" and a variable "int pos = 16;" in my c++ program. Is there a way using boost::regex to tell, if the character at location "pos - 2" is a letter "s"? I.e. a regex that matches the "s" character only and only if it's located at location "pos - 2", that's 16 - 2 = 14 in this case. Remember that variable "pos" can change at any time, but the regex itself cannot be changed. So I can't use a different regex for each word length... well technically I could, but I'm trying to shrink it down to just one entry.
Another example: the word "snakes" and variable pos = 12. We calculate 12 - 2 = 10 and we're expecting the character "s" at location 10. It does not appear because the word is too short, so there's no match. In the next word "traintracks" the letter "s" appears at location 11 and this doesn't match either. But in the string "badmotionstars" the "s" is present at location 10 and the word will match even though the word is longer than 12 characters.
Can this be done using regex? My only idea was to append a certain number of whitespaces to the word so that it reaches the length of "pos" before sending it off to regex matching.
Any ideas?
Last edited by displace; 08-28-2015 at 04:23 AM.
|