ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Are there any ways of using variables inside a regular expression? For example when matching a particular text I'd like to construct a regular expression in a dynamic way. In particular a part of the regular expression should change, but I do not know in advance what the change will be. All I have is a string variable that should become a part of the regex.
Take it I have a regular expression like "123someREGEX", but I want the word "some" to be variable. So is there a way I can 'inject' a variable inside the regex so that I can instead write the regex as "123\1REGEX". Of course this wouldn't work because "\1" is a backreference to a capturing group that doesn't exist. But perhaps there is a way to programatically create a number of named groups before the match takes place and then use those named groups as variables in the regex?
I deliberately haven't specified a language because I'd like to know, if any languages support this.
#include <iostream>
#include <string>
#include <regex>
int main (int argc, char *argv[])
{
std::string pattern { argv[1] };
static const std::regex re("(" + pattern + ")") ;
std::string line { "Now is the time for all good men to come to the aid of their party" };
std::smatch match ;
while(std::regex_search( line, match, re ) ) {
std::cout << match[1] << std::endl;
line = match.suffix().str();
}
}
Code:
$ clang++ -std=c++11 main.cpp && ./a.out the
the
the
the
import re
for line in text:
rx = '(%s%s%s)' % ('123',dynamicword,'REGEX')
dynrx = re.compile(rx,re.I)
for dynmatch in dynrx.findall(line):
print dynmatch
dynamicword = dynmatch
So basically I have to manually assemble the regex string programatically. There is no way to do it within the regex library itself (i.e. named groups)?
So basically I have to manually assemble the regex string programatically. There is no way to do it within the regex library itself (i.e. named groups)?
Manually and programatically are not the same. Write some pseudo-code with pseudo-data and it would be easier to provide an answer.
As far as I know you can do almost anything with regex and some code. Limited only by abilities and creativity.
You can do that in Perl, but I agree that providing a couple of examples of what you are trying to do would be helpful. http://perldoc.perl.org/perlretut.html
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.