Professional Web Applications Themes

regexp splitting problem - Ruby

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

  1. #1

    Default regexp splitting problem

    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




    Brett Guest

  2. #2

    Default Re: regexp splitting problem

    Brett S Hallett wrote: 
    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 /["']/. 
    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")
     
    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/>

    Maik Guest

  3. #3

    Default Re: regexp splitting problem


    "Brett S Hallett" <net.au> schrieb im Newsbeitrag
    news:net.au... 

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

    robert

    Robert Guest

  4. #4

    Default Re: regexp splitting problem

    Hi --

    On Mon, 1 Dec 2003, Robert Klemme wrote:
     
    >
    > txt = line.scan /"[^"]*" | '[^']*' | \S+/x[/ref]

    That preserves the quotation marks, which I don't think Brett wanted.

    The best I can do is the somewhat inelegant:

    line.scan(/(?:["']([^"']+)["'])|(\S+)/).flatten.compact


    David

    --
    David A. Black
    net


    David Guest

  5. #5

    Default Re: regexp splitting problem

    Brett S Hallett <net.au> wrote in message news:<net.au>... 

    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
    Alan Guest

Similar Threads

  1. Problem with text splitting into separate pieces
    By kentman69@adobeforums.com in forum Adobe Illustrator Windows
    Replies: 4
    Last Post: July 9th, 10:46 PM
  2. regexp problem
    By Taylor York in forum PHP Development
    Replies: 14
    Last Post: October 17th, 03:11 PM
  3. Splitting up an XML File
    By JAG in forum PERL Miscellaneous
    Replies: 5
    Last Post: September 19th, 02:16 AM
  4. regexp problem with UTF8
    By Risto Vaarandi in forum PERL Miscellaneous
    Replies: 0
    Last Post: July 16th, 03:03 PM
  5. Splitting a Database
    By Michael Volz in forum Microsoft Access
    Replies: 0
    Last Post: July 3rd, 12:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139