Regex newbie

Discussion in 'Perl Misc' started by gazza67, Mar 11, 2008.

  1. gazza67

    gazza67 Guest

    Anybody,

    I want to use a regex in which the string for which we are searching
    is a variable. Is this possible - it doesnt seem to work for me.

    $a = "be";

    $b="to be or not to be";
    if ($b =~ m/$a/i )
    print "this should be true";

    $b = "apples and oranges";
    if ($b =~ m/$a/i )
    print "this should be false";



    Cheers
    Gary
     
    gazza67, Mar 11, 2008
    #1
    1. Advertising

  2. gazza67

    kath Guest

    On Mar 11, 8:30 am, gazza67 <> wrote:
    > Anybody,
    >
    > I want to use a regex in which the string for which we are searching
    > is a variable. Is this possible - it doesnt seem to work for me.
    >
    > $a = "be";
    >
    > $b="to be or not to be";
    > if ($b =~ m/$a/i )
    > print "this should be true";
    >
    > $b = "apples and oranges";
    > if ($b =~ m/$a/i )
    > print "this should be false";
    >
    > Cheers
    > Gary


    Either you have to use post condition or have to close block after if
    statement with brackets

    print "this should be true" if ($b =~ /$a/i);

    or

    if ($b =~ /$a/i){
    print "this should be true";
    }

    sudhir.
     
    kath, Mar 11, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    716
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,639
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    607
  4. Xah Lee
    Replies:
    1
    Views:
    951
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    780
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page