# Regexp help

**URL:** <https://www.ruby-forum.com/t/regexp-help/188780>\
**Category:** Ruby\
**Created:** [May 18, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/regexp-help/188780 "2010-05-18T12:39:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [May 18, 2010, 12:39pm UTC](https://www.ruby-forum.com/t/regexp-help/188780/1 "2010-05-18T12:39:42Z")

</div>

Hi, I’m newbie to ruby, but I try searching a lot for my problem…

I need to extract from an enormous text file, in natural language, for  
two separated element in the same line and put them in a new file.

Example:  
IIS - IPSIASAR L. DA VINCI (XN\_\_\_\_01) - Responsabile: ENRICO FLAIANO  
VIA MONTE S. ROCCO, 15 PINETA SIGNORINI - 67100 L’AQUILA (AQ) - tel.  
0862 22112 - fax. 0862 22300 email : removed\_email\_address@domain.invalid -  
url:[http://www.istitutoleonardodavinciaq.191.it](http://www.istitutoleonardodavinciaq.191.it)

I need to extract the code (XN\_\_\_\_01) and the relative email  
“removed\_email\_address@domain.invalid”, put them in a CSV file or a sort of.

May I do this in ruby?

Thank’s very much for any help and so sorry for my bad english…

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [May 18, 2010, 1:38pm UTC](https://www.ruby-forum.com/t/regexp-help/188780/2 "2010-05-18T13:38:07Z")

</div>

On Tue, May 18, 2010 at 7:39 AM, Andrea C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to extract the code (XN\_\_\_\_01) and the relative email  
> “removed\_email\_address@domain.invalid”, put them in a CSV file or a sort of.
> 
> May I do this in ruby?

You can do it using a capture group:  
[http://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#regexp](http://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#regexp)  
and examples here: [Regex Tutorial - Parentheses for Grouping and Capturing](http://www.regular-expressions.info/brackets.html)

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [May 18, 2010, 2:36pm UTC](https://www.ruby-forum.com/t/regexp-help/188780/3 "2010-05-18T14:36:29Z")

</div>

Luis P. wrote:

> On Tue, May 18, 2010 at 7:39 AM, Andrea C.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I need to extract the code (XN\_\_\_\_01) and the relative email  
> > “removed\_email\_address@domain.invalid”, put them in a CSV file or a sort of.
> > 
> > May I do this in ruby?
> 
> You can do it using a capture group:  
> [Ruby Syntax](http://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#regexp)  
> and examples here: [Regex Tutorial - Parentheses for Grouping and Capturing](http://www.regular-expressions.info/brackets.html)

Thank you, but 'couse I’m quiet stupid, can you write me a pseudo-coded  
example? For the regex part, not the file open/write part.  
Thank’s again!

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [May 18, 2010, 7:40pm UTC](https://www.ruby-forum.com/t/regexp-help/188780/4 "2010-05-18T19:40:54Z")

</div>

Andrea C. wrote:

> Luis P. wrote:
> 
> > On Tue, May 18, 2010 at 7:39 AM, Andrea C.  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > I need to extract the code (XN\_\_\_\_01) and the relative email  
> > > “removed\_email\_address@domain.invalid”, put them in a CSV file or a sort of.
> > > 
> > > May I do this in ruby?
> > 
> > You can do it using a capture group:  
> > [Ruby Syntax](http://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#regexp)  
> > and examples here: [Regex Tutorial - Parentheses for Grouping and Capturing](http://www.regular-expressions.info/brackets.html)
> 
> Thank you, but 'couse I’m quiet stupid, can you write me a pseudo-coded  
> example? For the regex part, not the file open/write part.  
> Thank’s again!

Maybe this can help:

> **[Rubular](https://rubular.com/)**
>
> Ruby-based regular expression editor/tester

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [May 19, 2010, 4:24pm UTC](https://www.ruby-forum.com/t/regexp-help/188780/5 "2010-05-19T16:24:06Z")

</div>

2010/5/18 Andrea C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > [Ruby Syntax](http://ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#regexp)  
> > and examples here: [Regex Tutorial - Parentheses for Grouping and Capturing](http://www.regular-expressions.info/brackets.html)
> 
> Thank you, but 'couse I’m quiet stupid, can you write me a pseudo-coded  
> example? For the regex part, not the file open/write part.

It’s a bit difficult to guess which parts of your line are stable and  
which not, i.e. how to build the regexp. Can you post, say 5,  
different lines so we can recognize the pattern?

Kind regards

robert
