Regular Expression Help

Discussion in 'Ruby' started by eddieroger, Mar 13, 2007.

  1. eddieroger

    eddieroger Guest

    I'm trying to write a regular expression for Ruby and running into
    roadblocks. I'm not new to the concept of a regex, and have written
    many before, but I'm new to implementation in Ruby and working with a
    particularly pesky regex.

    The string I need to match is:
    *'''ABC'', [[Acronym Definition]]

    The only parts I want to change are the ABC and "Acronym Definition."
    So, the asterisk, quotes and brackets should be there, but - to make
    it harder - the brackets are optional.

    Thanks for the help!
    Eddie
    eddieroger, Mar 13, 2007
    #1
    1. Advertising

  2. eddieroger

    Alex Young Guest

    eddieroger wrote:
    > I'm trying to write a regular expression for Ruby and running into
    > roadblocks. I'm not new to the concept of a regex, and have written
    > many before, but I'm new to implementation in Ruby and working with a
    > particularly pesky regex.
    >
    > The string I need to match is:
    > *'''ABC'', [[Acronym Definition]]
    >
    > The only parts I want to change are the ABC and "Acronym Definition."
    > So, the asterisk, quotes and brackets should be there, but - to make
    > it harder - the brackets are optional.


    What's the output you're going for?

    --
    Alex
    Alex Young, Mar 13, 2007
    #2
    1. Advertising

  3. Hi --

    On 3/13/07, eddieroger <> wrote:
    > I'm trying to write a regular expression for Ruby and running into
    > roadblocks. I'm not new to the concept of a regex, and have written
    > many before, but I'm new to implementation in Ruby and working with a
    > particularly pesky regex.
    >
    > The string I need to match is:
    > *'''ABC'', [[Acronym Definition]]
    >
    > The only parts I want to change are the ABC and "Acronym Definition."
    > So, the asterisk, quotes and brackets should be there, but - to make
    > it harder - the brackets are optional.


    Maybe you could do something based on character classes; for example,
    in the one above, /(\W+)(\w+)(\W+)([\w\s]+)/ would give you ABC and
    Acronym Definition in $2 and $4. Whether you could use this for
    similar strings would depend of course on how predictable that pattern
    was.


    David

    --
    Q. What is THE Ruby book for Rails developers?
    A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
    Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
    A. Ruby Power and Light, LLC (http://www.rubypal.com)
    David A. Black, Mar 13, 2007
    #3
  4. eddieroger

    7stud 7stud Guest

    eddieroger wrote:
    > to make
    > it harder - the brackets are optional.
    >


    Then there has to be something else to signal the end of the Acronym
    Definition. You'll have to decide what you want that to be.

    --
    Posted via http://www.ruby-forum.com/.
    7stud 7stud, Mar 13, 2007
    #4
  5. eddieroger

    greg Guest

    / \*'''
    ([A-Z1-9]+) # acronym
    '',\s*
    (?:
    \[\[ ([\w\s]+) \]\] # acronym definition with matching optional
    parens
    |
    ([\w\s]+) # acronym definition without parens
    )
    /x


    ?: means don't capture what is in the parentheses
    This also assumes that there are either two brackets or zero (not one)
    Note that you will have to check to see if the match is in $2 or $3

    The most important thing about regular expressions is to be as
    specific as possible with what you specify, and think about all the
    possibilities of what will be matched and what will not be matched
    To match 'Acronym Definition' I am using [\w\s]+
    There is a good chance that this is overly broad for your application

    On Mar 13, 3:15 pm, "eddieroger" <> wrote:
    > I'm trying to write a regular expression for Ruby and running into
    > roadblocks. I'm not new to the concept of a regex, and have written
    > many before, but I'm new to implementation in Ruby and working with a
    > particularly pesky regex.
    >
    > The string I need to match is:
    > *'''ABC'', [[Acronym Definition]]
    >
    > The only parts I want to change are the ABC and "Acronym Definition."
    > So, the asterisk, quotes and brackets should be there, but - to make
    > it harder - the brackets are optional.
    >
    > Thanks for the help!
    > Eddie
    greg, Mar 14, 2007
    #5
    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. Anand

    Regular Expression help...

    Anand, Jul 9, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,213
    Eric J. Roode
    Jul 9, 2003
  2. Eric B.
    Replies:
    1
    Views:
    424
    Jim Gibson
    Dec 17, 2004
  3. VSK
    Replies:
    2
    Views:
    2,252
  4. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    822
    Alan Moore
    Dec 2, 2005
  5. GIMME
    Replies:
    3
    Views:
    11,908
    vforvikash
    Dec 29, 2008
Loading...

Share This Page