regex issue

Discussion in 'Ruby' started by Junkone, Jan 25, 2008.

  1. Junkone

    Junkone Guest

    my pattern should match either import or delete
    However it does not seem to be working.

    irb(main):014:0> pattern="([import]|[delete])"
    => "([import]|[delete])"
    irb(main):015:0> pattern.match("import")
    => #<MatchData:0x2e76f9c>
    irb(main):016:0> $1
    => nil
     
    Junkone, Jan 25, 2008
    #1
    1. Advertising

  2. Alle Friday 25 January 2008, Junkone ha scritto:
    > my pattern should match either import or delete
    > However it does not seem to be working.
    >
    > irb(main):014:0> pattern="([import]|[delete])"
    > => "([import]|[delete])"
    > irb(main):015:0> pattern.match("import")
    > => #<MatchData:0x2e76f9c>
    > irb(main):016:0> $1
    > => nil


    I see two problems with your code:
    1) if you want to check that a string (in your case, 'import') matches a
    pattern, you need to use

    string.match(pattern)

    which, in your case, is

    'import'.match(pattern)

    2) in a regexp, the construct [abc] means one character among 'a', 'b' or 'c',
    not (as I guess you think) the string 'abc'. Because of this, the string 'i'
    matches your pattern:

    'i'.match(pattern)
    => #<MatchData:0xb7bbed44>

    To do what you want, you simply need:

    pattern = "(import|delete")
    "import".match pattern
    =>#<MatchData:0xb7bb7cb0>
    $1
    => "import"

    I hope this helps

    Stefano
     
    Stefano Crocco, Jan 25, 2008
    #2
    1. Advertising

  3. On Jan 25, 2008 2:34 PM, Junkone <> wrote:
    > my pattern should match either import or delete
    > However it does not seem to be working.
    >
    > irb(main):014:0> pattern="([import]|[delete])"
    > => "([import]|[delete])"
    > irb(main):015:0> pattern.match("import")
    > => #<MatchData:0x2e76f9c>
    > irb(main):016:0> $1
    > => nil


    Try this:

    irb(main):004:0> pattern=/(import|delete)/
    => /(import|delete)/
    irb(main):005:0> pattern.match "import"
    => #<MatchData:0xb7cd1434>
    irb(main):006:0> $1
    => "import"

    Pattern should be a regular expression. Also I think you don't want
    square brackets, because that means matching any one character in the
    set, not the word.

    Jesus.
     
    Jesús Gabriel y Galán, Jan 25, 2008
    #3
  4. Junkone

    Lee Jarvis Guest

    You don't need parenthesis..

    >> pattern = 'import|delete'

    => "import|delete"
    >> 'import'.match(pattern)[0]

    => "import"
    >> 'delete'.match(pattern)[0]

    => "delete"


    Regards,
    Lee
    --
    Posted via http://www.ruby-forum.com/.
     
    Lee Jarvis, Jan 25, 2008
    #4
    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:
    724
    =?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,651
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    614
  4. Xah Lee
    Replies:
    1
    Views:
    955
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    795
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page