# Ruby Regex

**URL:** <https://www.ruby-forum.com/t/ruby-regex/165153>\
**Category:** Ruby\
**Created:** [April 9, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/ruby-regex/165153 "2009-04-09T16:44:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [April 9, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/ruby-regex/165153/1 "2009-04-09T16:44:06Z")

</div>

Hello,

I am working on extracting some data from a file.  
Sample data in the file are as follows:

Case 1: Direction abc,(xyz)  
Case 2: Direction fet

I want to extract the content after Direction but before the comma(if  
present).  
The issue I am facing is that the comma is not always present.

Otherwise a reg exp like:

reg\_exp = %r{Direction(.+),}

would have given me the content.  
This would not work in the 2nd case.

How to account for both the conditions.  
Any help would be appreciated…

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [April 9, 2009, 5:01pm UTC](https://www.ruby-forum.com/t/ruby-regex/165153/2 "2009-04-09T17:01:49Z")

</div>

Robert K. wrote:

> On 09.04.2009 16:44, Sriram V. wrote:
> 
> > present).  
> > How to account for both the conditions.  
> > Any help would be appreciated…
> 
> /Direction\s+([^,]+)/
> 
> Cheers
> 
> robert

Hey Robert,

Thanks a ton for ur help…  
It worked…

---

<div class="post-metadata">

**Author:** ![sriram](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/46a35a/32.png) [@sriram](https://www.ruby-forum.com/u/sriram)\
**Post date:** [April 9, 2009, 4:51pm UTC](https://www.ruby-forum.com/t/ruby-regex/165153/3 "2009-04-09T16:51:40Z")

</div>

On 09.04.2009 16:44, Sriram V. wrote:

> present).  
> How to account for both the conditions.  
> Any help would be appreciated…

/Direction\s+([^,]+)/

Cheers

robert
