# String scan question

**URL:** <https://www.ruby-forum.com/t/string-scan-question/91319>\
**Category:** Ruby\
**Created:** [April 6, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319 "2007-04-06T18:56:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/1 "2007-04-06T18:56:05Z")

</div>

this is probably elementary but I just havent found the right/  
reliable way to do this (that works always)

if a string has content in tags such as content goes here \</  
TagName\> whats the best way to put the content inside an array… the  
content can have whitespace chars (end of lines tabs etc) that should  
be preserved in the array element. These tags are simple (no  
properties).

I assume that the scan method is relevant but am having trouble  
constructing a regex that works reliably.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 7:00pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/2 "2007-04-06T19:00:09Z")

</div>

removed\_email\_address@domain.invalid wrote:

> constructing a regex that works reliably.  
> try

html\_file.scan(/(.+?)\</).flatten

This will put the text contents of all tags into an array.

Cheers,  
Peter

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 7:11pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/3 "2007-04-06T19:11:18Z")

</div>

Here is a simple test case

s = \nXXX\n\n\n  
\n Administrative Support - Supervisors\n\</  
VariableValue\>\n\n"

for the VariableValue tag, there should be 2 array elements \nXXX  
\n AND \n Administrative Support - Supervisors\n

s.scan(/(.+?)\</).flatten returns an empty array

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/4 "2007-04-06T19:28:21Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Here is a simple test case
> 
> s = \nXXX\n\n\n  
> \n Administrative Support - Supervisors\n\</  
> VariableValue\>\n\n"
> 
> for the VariableValue tag, there should be 2 array elements \nXXX  
> \n AND \n Administrative Support - Supervisors\n
> 
> s.scan(/(.+?)\</).flatten returns an empty array

Ah OK, I did not know there can be other tags inside. This works better:

s.scan(/(.+?)\</VariableValue\>/m).flatten

(note the ‘m’ flag for multiline)

however, to really match your example, I needed this:

s.scan(/(.+?)\</\n?VariableValue\>/m).flatten

are you sure there is a line break between / and the tag name?

Cheers,  
Peter

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 7:32pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/5 "2007-04-06T19:32:21Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Here is a simple test case
> 
> s = \nXXX\n\n\n  
> \n Administrative Support - Supervisors\n\</  
> VariableValue\>\n\n"  
> If it’s actually XML, just use REXML. Anything else is asking for  
> trouble, really.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/6 "2007-04-06T19:35:23Z")

</div>

No there isnt any (due to wrapping here)…  
I didnt know about the multiline option (seem to have missed that in  
the docs). that worked

thanks very much

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/7 "2007-04-06T20:01:07Z")

</div>

On Apr 6, 1:43 pm, Alex Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry, I didn’t notice that your tags aren’t matched. Is that  
> intentional? If so, ignore my suggestion - REXML clearly won’t work.
> 
> –  
> Alex

the content can be quite nonstandard and have mismatched tags etc  
(like real life html)… so this is not xml  
I will use rexml when the input is xml… thanks for your suggestion.  
this group is very useful for newbies.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/8 "2007-04-06T19:44:24Z")

</div>

Alex Y. wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > Here is a simple test case
> > 
> > s = \nXXX\n\n\n  
> > \n Administrative Support - Supervisors\n\</  
> > VariableValue\>\n\n"  
> > If it’s actually XML, just use REXML. Anything else is asking for  
> > trouble, really.  
> > Sorry, I didn’t notice that your tags aren’t matched. Is that  
> > intentional? If so, ignore my suggestion - REXML clearly won’t work.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 6, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/string-scan-question/91319/9 "2007-04-06T20:35:10Z")

</div>

btw, if the content is funky, you could still try Hpricot - it handles  
such crap surprisingly nicely, and unless you would like to match more  
complicated things than a text between an opening and closing tag, it  
will really make your life easier.

Cheers,  
Peter
