Regex confusion

Discussion in 'Perl Misc' started by trashman.horlicks@btinternet.com, Feb 19, 2007.

  1. Guest

    Hi everyone,
    I'm just starting to use regex to do some pattern matching but the
    syntax is a little confusing. I'm using some of
    the on-line regex checkers, but they all fail the following test:

    regex /\bTest\s*,/i
    look for Test, (or test, etc.)
    - result: failure

    I can't see anything wrong. Can anyone suggest whats amiss?

    TIA

    Paul
    , Feb 19, 2007
    #1
    1. Advertising

  2. tfe Guest

    Hi,

    You look to put a "," and you do not need.
    Regex looks like that: /regex/ , or !regex! or #regex# , etc...
    If you want to match the word "test", you juste have to put /\btest\b/
    to match it.

    The final option "i" comes after the separation :
    /\btest\b/i

    --
    tfe
    http://tfeserver.be

    On 19 fév, 16:55, wrote:
    > Hi everyone,
    > I'm just starting to use regex to do some pattern matching but the
    > syntax is a little confusing. I'm using some of
    > the on-line regex checkers, but they all fail the following test:
    >
    > regex /\bTest\s*,/i
    > look for Test, (or test, etc.)
    > - result: failure
    >
    > I can't see anything wrong. Can anyone suggest whats amiss?
    >
    > TIA
    >
    > Paul
    tfe, Feb 19, 2007
    #2
    1. Advertising

  3. Guest

    On 19 Feb, 16:07, "tfe" <> wrote:
    > Hi,
    >
    > You look to put a "," and you do not need.
    > Regex looks like that: /regex/ , or !regex! or #regex# , etc...
    > If you want to match the word "test", you juste have to put /\btest\b/
    > to match it.
    >
    > The final option "i" comes after the separation :
    > /\btest\b/i
    >
    > --
    > tfehttp://tfeserver.be
    >
    > On 19 fév, 16:55, wrote:
    >
    >
    >
    > > Hi everyone,
    > > I'm just starting to use regex to do some pattern matching but the
    > > syntax is a little confusing. I'm using some of
    > > the on-line regex checkers, but they all fail the following test:

    >
    > > regex /\bTest\s*,/i
    > > look for Test, (or test, etc.)
    > > - result: failure

    >
    > > I can't see anything wrong. Can anyone suggest whats amiss?

    >
    > > TIA

    >
    > > Paul- Hide quoted text -

    >
    > - Show quoted text -



    Thanks mate. I do actually need the "," to be a part of the regex
    string.

    By the way, looking at some examples of regex, I see constructs like
    this:
    [_\W]{0,3}
    Isn't this just the same as [\w\W]{0,3} ?
    , Feb 19, 2007
    #3
  4. Guest

    Sorry, got confused. What I meant to say was: I am trying to construct
    a regex that will check for
    "test," (not the quotes)
    "Test,"
    "Test ,"
    "test ,"
    "Test ,"
    plus any combination of upper and lower cases, plus whitespace between
    "test" and ","

    TIA

    Paul
    , Feb 19, 2007
    #4
  5. kens Guest

    On Feb 19, 10:55 am, wrote:
    > Hi everyone,
    > I'm just starting to use regex to do some pattern matching but the
    > syntax is a little confusing. I'm using some of
    > the on-line regex checkers, but they all fail the following test:
    >
    > regex /\bTest\s*,/i
    > look for Test, (or test, etc.)
    > - result: failure
    >
    > I can't see anything wrong. Can anyone suggest whats amiss?
    >
    > TIA
    >
    > Paul


    Post actual code & sample input.
    Such as:

    use strict;
    use warnings;

    while (<DATA>)
    {
    chomp();
    if ( /\bTest\s*,/i )
    {
    print "MATCHED: >>$_<<\n";
    }
    else
    {
    print "NOT MATCHED: >>$_<<\n";
    }
    }

    __DATA__
    test,
    Test,
    Test ,
    test ,
    Test ,

    Ken
    kens, Feb 19, 2007
    #5
  6. wrote:
    >
    > By the way, looking at some examples of regex, I see constructs like
    > this:
    > [_\W]{0,3}
    > Isn't this just the same as [\w\W]{0,3} ?


    No. [_\W] says match an underscore or a non-word character. You could do the
    same thing with the POSIX character class [^[:alnum:]].

    [\w\W] matches *any* character, as would [\s\S] or [\d\D] or (?s:.).




    John
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Feb 19, 2007
    #6
  7. <> wrote:

    > I'm just starting to use regex to do some pattern matching



    You need *two* pieces of information to analyse why a pattern is
    matching or not.

    You need the regular expression, and you need the string that the
    regular expression is to be matched against.


    > regex /\bTest\s*,/i



    There is the regular expression.


    > I can't see anything wrong.



    Neither can we, because we cannot see what is in $_


    > Can anyone suggest whats amiss?



    Probably the string does not match the pattern somehow. (heh)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 20, 2007
    #7
    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:
    688
    =?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,604
    Ant...
    Nov 6, 2003
  3. John Hunter

    regex confusion

    John Hunter, Dec 9, 2003, in forum: Python
    Replies:
    8
    Views:
    343
    John Hunter
    Dec 9, 2003
  4. Replies:
    3
    Views:
    726
    Reedick, Andrew
    Jul 1, 2008
  5. guthrie

    Regex confusion...

    guthrie, Sep 27, 2007, in forum: Perl Misc
    Replies:
    6
    Views:
    100
    comp.llang.perl.moderated
    Sep 28, 2007
Loading...

Share This Page