# String scan question

**URL:** <https://rubytalk.org/t/string-scan-question/36742>\
**Category:** ruby-talk\
**Created:** [6 April 2007 16:55 UTC](https://rubytalk.org/t/string-scan-question/36742 "2007-04-06T16:55:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srinsriram](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Srinsriram](https://rubytalk.org/u/Srinsriram)\
**Post date:** [6 April 2007 16:55 UTC](https://rubytalk.org/t/string-scan-question/36742/1 "2007-04-06T16:55:06Z")

</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 \<TagName\> content goes here \</

> 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:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [6 April 2007 16:59 UTC](https://rubytalk.org/t/string-scan-question/36742/2 "2007-04-06T16:59:49Z")

</div>

srinsriram@gmail.com wrote:

> 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 \<TagName\> content goes here \</  
> \> 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.

try

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

This will put the text contents of all \<TagName\> tags into an array.

Cheers,  
Peter

> **···**
>
> --  
> [http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
> [http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
> [http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![Srinsriram](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Srinsriram](https://rubytalk.org/u/Srinsriram)\
**Post date:** [6 April 2007 17:10 UTC](https://rubytalk.org/t/string-scan-question/36742/3 "2007-04-06T17:10:09Z")

</div>

Here is a simple test case

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

> \n\n"

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

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

> **···**
>
> On Apr 6, 12:59 pm, Peter Szinek \<p...@rubyrailways.com\> wrote:
> 
> > srinsri...@gmail.com wrote:  
> > \> 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 \<TagName\> content goes here \</  
> > \> \> 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.
> > 
> > try
> > 
> > html\_file.scan(/\<TagName\>(.+?)\</).flatten
> > 
> > This will put the text contents of all \<TagName\> tags into an array.
> > 
> > Cheers,  
> > Peter
> > 
> > --[http://www.rubyrailways.com](http://www.rubyrailways.com):: Ruby and Web2.0 bloghttp://scrubyt.org:: Ruby web scraping frameworkhttp://rubykitchensink.ca/:: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [6 April 2007 17:27 UTC](https://rubytalk.org/t/string-scan-question/36742/4 "2007-04-06T17:27:02Z")

</div>

srinsriram@gmail.com wrote:

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

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

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

(note the 'm' flag for multiline)

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

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

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

Cheers,  
Peter

> **···**
>
> --  
> [http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
> [http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
> [http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [6 April 2007 17:31 UTC](https://rubytalk.org/t/string-scan-question/36742/5 "2007-04-06T17:31:06Z")

</div>

srinsriram@gmail.com wrote:

> Here is a simple test case
> 
> s = \<VariableValue\>\nXXX\n\</VariableValue\>\n\n\<VariableValue\>  
> \n\<Choice\> Administrative Support - Supervisors\<Choice\>\n\</  
> \>\n\n"

If it's actually XML, just use REXML. Anything else is asking for trouble, really.

> **···**
>
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Srinsriram](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Srinsriram](https://rubytalk.org/u/Srinsriram)\
**Post date:** [6 April 2007 17:35 UTC](https://rubytalk.org/t/string-scan-question/36742/6 "2007-04-06T17:35:04Z")

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

> **···**
>
> On Apr 6, 1:27 pm, Peter Szinek \<p...@rubyrailways.com\> wrote:
> 
> > srinsri...@gmail.com wrote:  
> > \> Here is a simple test case
> > 
> > \> s = \<VariableValue\>\nXXX\n\</VariableValue\>\n\n\<VariableValue\>  
> > \> \n\<Choice\> Administrative Support - Supervisors\<Choice\>\n\</  
> > \> \>\n\n"
> > 
> > \> for the VariableValue tag, there should be 2 array elements \nXXX  
> > \> \n AND \n\<Choice\> Administrative Support - Supervisors\<Choice\>\n
> > 
> > \> s.scan(/\<VariableValue\>(.+?)\</).flatten returns an empty array
> > 
> > Ah OK, I did not know there can be other tags inside. This works better:
> > 
> > s.scan(/\<VariableValue\>(.+?)\<\/VariableValue\>/m).flatten
> > 
> > (note the 'm' flag for multiline)
> > 
> > however, to really match your example, I needed this:
> > 
> > s.scan(/\<VariableValue\>(.+?)\<\/\n?VariableValue\>/m).flatten
> > 
> > are you sure there is a line break between / and the tag name?
> > 
> > Cheers,  
> > Peter
> > 
> > --[http://www.rubyrailways.com](http://www.rubyrailways.com):: Ruby and Web2.0 bloghttp://scrubyt.org:: Ruby web scraping frameworkhttp://rubykitchensink.ca/:: The indexed archive of all things Ruby

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [6 April 2007 17:43 UTC](https://rubytalk.org/t/string-scan-question/36742/7 "2007-04-06T17:43:06Z")

</div>

Alex Young wrote:

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

---

<div class="post-metadata">

**Author:** ![Srinsriram](https://avatars.discourse-cdn.com/v4/letter/s/ee59a6/32.png) [@Srinsriram](https://rubytalk.org/u/Srinsriram)\
**Post date:** [6 April 2007 18:00 UTC](https://rubytalk.org/t/string-scan-question/36742/8 "2007-04-06T18:00:07Z")

</div>

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.

> **···**
>
> On Apr 6, 1:43 pm, Alex Young \<a...@blackkettle.org\> wrote:
> 
> > Alex Young wrote:  
> > \> srinsri...@gmail.com wrote:  
> > \>\> Here is a simple test case
> > 
> > \>\> s = \<VariableValue\>\nXXX\n\</VariableValue\>\n\n\<VariableValue\>  
> > \>\> \n\<Choice\> Administrative Support - Supervisors\<Choice\>\n\</  
> > \>\> \>\n\n"  
> > \> If it's actually XML, just use REXML. Anything else is asking for  
> > \> trouble, really.
> > 
> > Sorry, I didn't notice that your \<Choice\> tags aren't matched. Is that  
> > intentional? If so, ignore my suggestion - REXML clearly won't work.
> > 
> > --  
> > Alex

---

<div class="post-metadata">

**Author:** ![Peter\_Szinek3](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Peter\_Szinek3](https://rubytalk.org/u/Peter_Szinek3)\
**Post date:** [6 April 2007 18:33 UTC](https://rubytalk.org/t/string-scan-question/36742/9 "2007-04-06T18:33:54Z")

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

> **···**
>
> --  
> [http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
> [http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
> [http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby
