Regular Expression Help

E

eddieroger

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
 
A

Alex Young

eddieroger said:
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?
 
D

David A. Black

Hi --

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)
 
7

7stud 7stud

eddieroger said:
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.
 
G

greg

/ \*'''
([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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top