# Regular Expression Help

**URL:** <https://www.ruby-forum.com/t/regular-expression-help/88736>\
**Category:** Ruby\
**Created:** [March 13, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/regular-expression-help/88736 "2007-03-13T22:55:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddieroger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@eddieroger](https://www.ruby-forum.com/u/eddieroger)\
**Post date:** [March 13, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/regular-expression-help/88736/1 "2007-03-13T22:55:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eddieroger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@eddieroger](https://www.ruby-forum.com/u/eddieroger)\
**Post date:** [March 14, 2007, 12:32am UTC](https://www.ruby-forum.com/t/regular-expression-help/88736/2 "2007-03-14T00:32:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eddieroger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@eddieroger](https://www.ruby-forum.com/u/eddieroger)\
**Post date:** [March 13, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/regular-expression-help/88736/3 "2007-03-13T22:55:54Z")

</div>

eddieroger wrote:

> it harder - the brackets are optional.  
> What’s the output you’re going for?

---

<div class="post-metadata">

**Author:** ![eddieroger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@eddieroger](https://www.ruby-forum.com/u/eddieroger)\
**Post date:** [March 14, 2007, 4:10am UTC](https://www.ruby-forum.com/t/regular-expression-help/88736/4 "2007-03-14T04:10:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eddieroger](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/edb3f5/32.png) [@eddieroger](https://www.ruby-forum.com/u/eddieroger)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/regular-expression-help/88736/5 "2007-09-25T23:04:57Z")

</div>

Hi –

On 3/13/07, eddieroger [removed\_email\_address@domain.invalid](mailto: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
