regex

This is a discussion on regex within the C++ Programming forums, part of the General Programming Boards category; I have a string with some text for instance: string str = "this is some text $variable some more text ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    regex

    I have a string with some text for instance:

    string str = "this is some text $variable some more text $random[1,6]";

    Now I want to replace variables that are in map <string, string>.. For instance replace $variable (map->first) with map->second.. and when replacing $random, call a function randomize (in range) and then replace variable with lets say 439204..

    I decided to go for boost::regex, but Im unsure what would be the best approach, since I dont have to call random function if I dont have that variable in string.. What do you think would be the best approach for this?

    Thanks a lot for help

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    If regex tools can't do it, do it yourself.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    The best solution is to use a language that doesn't make you jump through hoops to accomplish this kind of thing.

    Code:
    #!/usr/bin/perl -w
    
    sub func ($);
    
    %repl = ('$variable' => 'urmfga');
    $string = 'this is some text $variable some more text $random[1,6]';
    
    $string =~ s/(\$\S+)/func($1)/ge;
    
    print $string;
    
    sub func ($)
    {
    	if ($_[0] =~ /\$random\[(\d+),(\d+)\]/)
    	{
    		int(rand($2 - $1) + $1);
    	}
    	elsif (exists $repl{$_[0]})
    	{
    		$repl{$_[0]};
    	}
    	else
    	{
    		warn "Failure <$_[0]>\n";
    		$_[0];
    	}
    }
    Last edited by zx-1; 10-09-2006 at 06:43 AM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    zx-1, this is C++ forum. Not a place for scripting languages.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by maxorator
    zx-1, this is C++ forum. Not a place for scripting languages.
    If you had sat down and contemplated my post, you might have come to the realization that the point I was trying to make was that C/C++ is not the panacea some people think it is. I.e. provided the OP isn't forced to use C++ his problem can be solved with a lot less effort in a language more attuned to it, like Perl, Python, or Ruby.

    (Edited language to be somewhat less confrontational)
    Last edited by zx-1; 10-09-2006 at 07:10 AM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I guess he wants to use that in his program. Since this is a C++ forum, the rest of the program is in C++.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    It is not hard to integrate Perl with C/C++. Maxorator, get your hackles down bro, zx-1 was just showing that C++ may not be the best tool for the job on this one.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Integrate... You mean, install Perl to every computer that the computer runs on?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Maybe start with a simple loop which finds '$' in a string, then extracts the word which follows.
    Based on the value of the word, do various things to get a replacement text and then substitute.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Boost Regex in Eclipse CDT
    By Hunter0000 in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2010, 11:22 AM
  2. regex help
    By Jaqui in forum Tech Board
    Replies: 8
    Last Post: 10-14-2006, 03:53 PM
  3. My own regex...............class?
    By misplaced in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2005, 09:18 AM
  4. <regex.h> regex syntax in C
    By battersausage in forum C Programming
    Replies: 7
    Last Post: 03-24-2004, 12:35 PM
  5. How is regex used?
    By Strider in forum C++ Programming
    Replies: 0
    Last Post: 12-14-2001, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21