LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2015, 04:16 AM   #1
displace
Member
 
Registered: Jan 2013
Location: EU
Distribution: Debian
Posts: 268

Rep: Reputation: 25
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.
 
Old 08-28-2015, 05:16 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 08-28-2015, 05:49 AM   #3
displace
Member
 
Registered: Jan 2013
Location: EU
Distribution: Debian
Posts: 268

Original Poster
Rep: Reputation: 25
Yes, so how do I make regex parser know that "pos" is a variable in c++?
 
Old 08-28-2015, 06:29 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 08-28-2015, 07:40 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
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/
 
Old 08-28-2015, 09:11 AM   #6
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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')
 
  


Reply

Tags
regex



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regular expressions schmitta Linux - Newbie 363 09-09-2019 04:44 PM
Regular expressions Khaj.pandey Linux - Newbie 19 04-21-2010 11:09 PM
help with regular expressions mariogarcia Linux - Software 3 01-28-2009 03:23 AM
Regular Expressions overbored Linux - Software 3 06-24-2004 02:34 PM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration