Ruby Regex

S

Sriram Varahan

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...
 
R

Robert Klemme

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

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

Cheers

robert
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top