Results 1 to 6 of 6

Thread: Extracting an element forming part of a variable in a regexp

  1. #1
    Join Date
    Jan 2010
    Posts
    62

    Extracting an element forming part of a variable in a regexp

    I am developing a tool currently in Perl, and I need to extract characters from a variable that I put in a regexp. I illustrate directly, it will be clearer.
    Code:
    my $req = 'select machine from device where machine = $1';
    
    my $separator = "\$";
    
    my $variable = "foo";
    $1 is not a variable, but the separator followed by the numeral "1". The separator is intended to be replaced by the variable thereafter that it contains in a string. I put "foo" for example. I then have the following regexp:
    Code:
    $req =~ s/\Q$sep/$var/;
    
    print "reqfinal = $req\n"; # select code from client where code = foo1
    This makes it possible to replace my separator variable. But what I also do is extract the "1" in a variable that I can use later, and also make sure to have that:
    Code:
    # select code from client where code = toto
    in my result, without the "1" then.

  2. #2
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Extracting an element forming part of a variable in a regexp

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    my $req = 'select code from client where code = $1';
    my $sep = '\$';
    my $var = "foo"; 
    if ($req =~ m/(.*?)$sep(\d+)/){
    	$req = $1.$var;
    	my $num = $2;
    	print "req : $req\tnum = $num\n";
    	# req : select code from client where code = foo	num = 1
    }

  3. #3
    Join Date
    Jan 2010
    Posts
    62

    Re: Extracting an element forming part of a variable in a regexp

    It looks pretty good, but I can not make it work. Or rather, the print is blank, despite the fact that the code is good. Can I get more information about the code specially:
    Code:
    if ($req =~ m/(.*?)$sep(\d+)/){
    For "select code from client where code ="?

    I know the operation of each, but I do not know how well together they work.

  4. #4
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Extracting an element forming part of a variable in a regexp

    Search of
    Code:
    (.*?)$sep(\d+)
    .*? any character from 0 to x times "select code from client where code = "

    The question mark is used to make non-greedy search (in this instance, it is useless but I used to use with .*).

    followed by $sep $

    followed by \d+ of 1 to x digits 1

    Parentheses are used to retrieve data in $1 and $2

    It does something that you do not understand?

  5. #5
    Join Date
    Jan 2010
    Posts
    62

    Re: Extracting an element forming part of a variable in a regexp

    I admit that it was mainly the use of "?" who challenged me, thinking that ".*" is enough. I understand the rest. Thank you very much, it should work even if my display is failing yet. I reported there after debugging, once it is operational. So for now, it does not work. It does not gives me the contents if (the prints).

    I fear that the contents of "$sep" is the character "$" does not involve the end of the string, and suddenly my game is not verified, not only making the contents of if.

    Any idea?

    Actually, when I put anything other than a dollar separator, such as "%" for example, the regexp works. I would therefore resolve the particular case of the "$" as a separator.

  6. #6
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Extracting an element forming part of a variable in a regexp

    In my code that I gave you works well with the $. And also if used with single quotes?
    Code:
    my $sep = '\$';
    If you leave the double quotes open when you initialize the variable, backslash is deleted.

Similar Threads

  1. Replies: 1
    Last Post: 10-05-2012, 11:55 AM
  2. Forming Nations into Supreme Ruler: Cold War
    By Adamya in forum Video Games
    Replies: 5
    Last Post: 20-07-2011, 11:26 PM
  3. Replies: 5
    Last Post: 18-06-2011, 07:39 AM
  4. Extracting string (RegExp)
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 03:44 AM
  5. Replies: 2
    Last Post: 28-08-2009, 07:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,565,845.67046 seconds with 17 queries