LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with regular expressions (https://www.linuxquestions.org/questions/programming-9/help-with-regular-expressions-4175551972/)

displace 08-28-2015 04:16 AM

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?

grail 08-28-2015 05:16 AM

I am not familiar with using boost::regex, but if I were to do this irrelevant of language, the regex would be something like:
Code:

.{pos-3}s
So here we want up to the position prior to the one we want and then the letter we are looking for (in this case, 's')

The curly brackets sets a range and here we just want pos-3 lots of any character

displace 08-28-2015 05:49 AM

Yes, so how do I make regex parser know that "pos" is a variable in c++?

grail 08-28-2015 06:29 AM

Well assuming it can use the {}, only numbers are expected in there so for me it would be a suck it and see approach not knowing much more about it :)

pan64 08-28-2015 07:40 AM

You do not need regexp but string[pos-2] or string.at(pos-2)
using regexp you need to construct first the regexp itself using pos and than you can use it. https://bobobobo.wordpress.com/2009/...c-boost-regex/

Guttorm 08-28-2015 09:11 AM

I agree with pan64. Just remember to check length first so you don't get exceptions. Something like this:

Code:

if (str.length() >= pos - 3 && str.at(pos-3) == 's')


All times are GMT -5. The time now is 04:11 AM.