Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Regular Expression Help

Reply
Thread Tools

Regular Expression Help

 
 
eddieroger
Guest
Posts: n/a
 
      03-13-2007
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

 
Reply With Quote
 
 
 
 
Alex Young
Guest
Posts: n/a
 
      03-13-2007
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

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      03-13-2007
Hi --

On 3/13/07, eddieroger <(E-Mail Removed)> 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)

 
Reply With Quote
 
7stud 7stud
Guest
Posts: n/a
 
      03-13-2007
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/.

 
Reply With Quote
 
greg
Guest
Posts: n/a
 
      03-14-2007
/ \*'''
([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" <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments