Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > regexp splitting problem

Reply
Thread Tools

regexp splitting problem

 
 
Brett S Hallett
Guest
Posts: n/a
 
      11-29-2003
Hi,
I am trying to split the following line of text:

<button> "btn Exit" "Exit Button" ( note the quotes may be
" or ' , read from a file)

in such a way that I can say

txt = line.split(/regrex/)

and get back

txt[0] = <button>
txt[1] = btn Exit
txt[2] = Exit Button

my current regexp

ans = tst.split(/[\"|\']/)

does this , except that the last set is missing ! ,


txt[0] = <button>
txt[1] = btn Exit
txt[2] =

so how do I get the expression to continue processing the line ??
Thanks




 
Reply With Quote
 
 
 
 
Maik Schmidt
Guest
Posts: n/a
 
      11-29-2003
Brett S Hallett wrote:
>
> ans = tst.split(/[\"|\']/)

Your regex can be simplified, because within a character class the pipe
character means "match a pipe character" and not "or". Additionally, you
do not have to escape the quotes, so the resulting regex would be /["']/.
>
> does this , except that the last set is missing ! ,
>

That's not totally correct. The last set isn't missing, but the 3rd set
is empty. For easier debugging try:

puts text.split(/["']/).join("\n")

> so how do I get the expression to continue processing the line ??

As mentioned before: That isn't the problem. Your are searching for a
regex that splits a line into tokens. Some of the tokens are enclosed in
quotes and some are not. Both tokens can contain whitespace. I am not
sure, if your problem can easily be solved by using a single regex. If
you can, you should change your input format.

Is the first token always enclosed in [<>] characters? Are the following
tokens always enclosed in quotes? Then it would be easier to split the
line, but you still would need more than one split call. Maybe then it
would fit in a single call of scan?

Cheers,

<maik/>

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-01-2003

"Brett S Hallett" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Hi,
> I am trying to split the following line of text:
>
> <button> "btn Exit" "Exit Button" ( note the quotes may be
> " or ' , read from a file)
>
> in such a way that I can say
>
> txt = line.split(/regrex/)
>
> and get back
>
> txt[0] = <button>
> txt[1] = btn Exit
> txt[2] = Exit Button
>
> my current regexp
>
> ans = tst.split(/[\"|\']/)
>
> does this , except that the last set is missing ! ,
>
>
> txt[0] = <button>
> txt[1] = btn Exit
> txt[2] =
>
> so how do I get the expression to continue processing the line ??


txt = line.scan /"[^"]*" | '[^']*' | \S+/x

robert

 
Reply With Quote
 
Alan Chen
Guest
Posts: n/a
 
      12-03-2003
Brett S Hallett <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Hi,
> I am trying to split the following line of text:
>
> <button> "btn Exit" "Exit Button" ( note the quotes may be
> " or ' , read from a file)
>
> in such a way that I can say
>
> txt = line.split(/regrex/)
>
> and get back
>
> txt[0] = <button>
> txt[1] = btn Exit
> txt[2] = Exit Button


This works for your example, but may be somewhat fragile when you go
to expand its use over a wider range of inputs...

require 'test/unit'

class TC_one < Test::Unit::TestCase
def test_01
str = %Q/<button> "btn Exit" "Exit Button"/
ans = str.split( / *[\"\'] *\"?/)

assert_equal( ["<button>", "btn Exit", "Exit Button"], ans)
end
end

Cheers,
- alan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[regexp] How to convert string "/regexp/i" to /regexp/i - ? Joao Silva Ruby 16 08-21-2009 05:52 PM
Regexp: unexspected splitting of string in several groups Piet Python 3 06-02-2004 09:59 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments