LinuxQuestions.org
Review your favorite Linux distribution.
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 09-07-2015, 08:05 AM   #1
displace
Member
 
Registered: Jan 2013
Location: EU
Distribution: Debian
Posts: 268

Rep: Reputation: 25
Help with regular expressions [2]


Hey, I'm just gonna be brief about it.

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.
 
Old 09-07-2015, 09:53 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Yeah, it's possible. Check out this example with sed:
Code:
$ foo="a word"
Code:
$ echo "foobar" | sed "s/foo/this is $foo /"
this is a word bar
Note the double quotation marks in sed, otherwise the use of variables will fail.

Edit:
The same applies for grep:
Code:
$ bar="string"

$ echo "A long string with basically nothing in it" | grep -o "long $bar"
long string

Best regards,
HMW

Last edited by HMW; 09-07-2015 at 09:59 AM.
 
Old 09-07-2015, 12:12 PM   #3
displace
Member
 
Registered: Jan 2013
Location: EU
Distribution: Debian
Posts: 268

Original Poster
Rep: Reputation: 25
It's probably also possible to do with awk scripting language, but what about higher languages like c++ (boost::regex) and python?
 
Old 09-07-2015, 01:27 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
Here's a simple c++ example:
Code:
#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
Code:
$./a.out "\bthe\b"
the
the
 
Old 09-07-2015, 07:18 PM   #5
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
In python you could do


Code:
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
 
Old 09-09-2015, 01:28 AM   #6
displace
Member
 
Registered: Jan 2013
Location: EU
Distribution: Debian
Posts: 268

Original Poster
Rep: Reputation: 25
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)?
 
Old 09-09-2015, 01:42 AM   #7
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Quote:
Originally Posted by displace View Post
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.
 
Old 09-10-2015, 03:48 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
  


Reply



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
Help with regular expressions displace Programming 5 08-28-2015 09:11 AM
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

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

All times are GMT -5. The time now is 02:03 AM.

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