Results 1 to 17 of 17

Thread: perl: Split a string?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517

    perl: Split a string?

    Im stuck trying to split this string

    It must split all of my examples. I want to seperate them by the character by these three characters

    ?:;


    1?Not Insured:Insured;
    1?No:Yes;
    1?option1:option2;

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use a regular expression.
    Code:
    $input =~ /^(.+)\?(.+):(.+);$/
    // $1, $2 and $3 hold the parts
    I hope I got string begin and end right (^ and $ I think, but I'm not sure).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    You have been some help, but that code doesnt do what I need. So let me try again.

    Say I have a html page in a scalar. $html
    in this page could be the string 1?Not Insured:Insured;

    So I want to go threw the HTML and loop threw and put all of these expressions into 3 strings

    $inp1 = 1;
    $inp2 = "Not Insured";
    $inp3 = "Insured";


    Can you help me? Thanks so much!

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    What does $html contain? Some random HTML code, with this stuff embedded? Or only such triples? What more do we know about these triples except that they are separated by ? and : and finished by ;? Are they all on their own line? Is the first part always a number?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Its an entire html page, and these are embedded in this page.

    They ALWAYS are in this format


    number?option1:option2;


    number is going to be which option will show up, and im gonna erase the rest of the string in the html. Get it?

    So if its 1?No:Yes; it will say Yes
    and 0?No:Yes; will say No

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Hmmm....

    There's something with execution in the second part of search-replace regular expressions that should do this nearly by itself. But I'm no expert in perl.

    It's something like
    $html =~ s/(\d)?(.+?):(.+?);/$1 ? $2 : $3/egs;

    I believe this should work.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    Wow!

    It ate my html up like roast beef, and them spit out stuff all over the place. Funny but kinda sad!

    Heh.. Any idea why it would do that?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    $html =~ s/(\d)\?(.+?):(.+?);/$1 ? $2 : $3/egs;


    You forgot to escape the ?

    Thanks alot!!!

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    I made a note to my self of how you managed to do that with that regex.

    Is my understanding correct?


    #NOTE: This line swaps out expressions in the html that
    #look like this
    #num?option1?option2
    #if num = 1 then uses option2, else option1

    #switch / var1(digit) escape? var2(all) : var3(all) ; / if 1 then 3, else 2 / right=expression, get all, single line input
    $html =~ s/(\d)\?(.+?):(.+?);/$1 ? $2 : $3/egs;

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Exactly.

    Except that the "all" is "one or more of any character, use the least possible"
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    So . is all char
    + is one or more, and ? is what

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    . means any char, + means one or more, the ? makes + non-greedy.

    Consider this string:
    blabla&foobar&useless
    and this regular expression
    (.+)&(.+)
    Any character, 1 or more times, followed by &, followed by any character 1 or more times.
    Now, there are two ways to match this. Way #1:
    $1 = blabla&foobar
    $2 = useless
    Way #2:
    $1 = blabla
    $2 = foobar&useless

    Which should apply? The standard behavior is to be greedy, that is, to match as much as possible while still matching the expression as a whole. This means that the first .+ matches everything up to the second &, which is more than matching up to the first &, so Way #1 will be chosen by default,
    The ? after the + makes it non-greedy, so that it matches as little as possible. Way #2 will be chosen for the regular expression
    (.+?)&(.+)

    The reason why I have these in your case is simple. If your $html looks like

    Some characters1?one:other;some more characters0?other:one;and some final characters.

    Then a greedy match would grab
    $1 = 1
    $2 = one:other;some more characters0?other
    $3 = one

    Which is not what you want.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    oh, that makes so much more sense now.
    I wondered what 'greedy' was.

    thanks alot for the help. im sure I will be asking
    you lots more later =)

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517

    problem

    Sometimes there is no number, and it doesnt get the string.

    So is it possible if there is no number to act like its 0 ???

    how would you code that?

    i tried this:
    $html =~ s/(\d|)\?(.+?):(.+?);/$1 ? $2 : $3/egs;

    the pipe didnt help.

    Thanks in advance!

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    $html =~ s/(\d?)\?(.+?):(.+?);/$1 ? $2 : $3/egs;

    The ? here means "0 or 1 times".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    so where did you learn the regex so well, I do for work
    everyday ,so I want to learn it well.


    Whats the best way?

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No idea actually. I read through a few tutorials and simply used them.

    I found the JavaScript Regex reference by Netscape to be quite good, or the perl reference (manpages).

    If you've got some *nix system, just type man perl and check which chapter you need.

    On Windows, there might have come a reference with your Perl installation.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width