Forum: Ruby Regular Expression Help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
eddieroger (Guest)
on 2007-03-13 23:55
(Received via mailing list)
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
Alex Y. (Guest)
on 2007-03-13 23:55
(Received via mailing list)
eddieroger wrote:
> it harder - the brackets are optional.
What's the output you're going for?
7stud -. (Guest)
on 2007-03-14 01:32
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.
greg (Guest)
on 2007-03-14 05:10
(Received via mailing list)
/   \*'''
    ([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
David A. Black (Guest)
on 2007-09-26 01:04
(Received via mailing list)
Hi --

On 3/13/07, eddieroger <removed_email_address@domain.invalid> wrote:
> 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
This topic is locked and can not be replied to.