how to remove the string part between the two marks

Discussion in 'Perl Misc' started by Beno, Mar 9, 2011.

  1. Beno

    Beno Guest

    how to remove the string part between the two marks
    for example
    from string "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>"
    extract the text "12:30"

    I succeeded so I made the first split the string with the word
    <SPAN ID=TIME> then again split the string with the word </ SPAN>

    works but is complicated:

    $ string = "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>";
    @ part1 = split (/ <SPAN ID=TIME> /, $ string);
    @ part2 = split (/ <\ / SPAN> /, $ part1 [0]);
    print part2 [0];

    I know that there is an easier way in Perl, but how?

    Please help.
    Thanks.

    Beno
     
    Beno, Mar 9, 2011
    #1
    1. Advertising

  2. Beno

    Beno Guest

    Re: how to extract the string part between the two marks

    Not to remove, I need to extract the tekst 12:30

    > how to remove the string part between the two marks
    > for example
    > from string "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>"
    > extract the text "12:30"
    >
    > I succeeded so I made the first split the string with the word
    > <SPAN ID=TIME> then again split the string with the word </ SPAN>
    >
    > works but is complicated:
    >
    > $ string = "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>";
    > @ part1 = split (/ <SPAN ID=TIME> /, $ string);
    > @ part2 = split (/ <\ / SPAN> /, $ part1 [0]);
    > print part2 [0];
    >
    > I know that there is an easier way in Perl, but how?
    >
    > Please help.
    > Thanks.
    >
    > Beno
    >
     
    Beno, Mar 9, 2011
    #2
    1. Advertising

  3. On 09.03.2011 13:30, Beno wrote:
    > how to remove the string part between the two marks
    > for example
    > from string "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>"
    > extract the text "12:30"
    >
    > I succeeded so I made the first split the string with the word
    > <SPAN ID=TIME> then again split the string with the word </ SPAN>


    This looks like html.

    perldoc -q html

    - Wolf
     
    Wolf Behrenhoff, Mar 9, 2011
    #3
  4. Beno

    smallpond Guest

    Re: how to extract the string part between the two marks

    On Mar 9, 7:32 am, "Beno" <> wrote:
    > Not to remove, I need to extract the tekst 12:30
    >
    > > how to remove the string part between the two marks
    > > for example
    > > from string "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>"
    > > extract the text "12:30"

    >
    > > I succeeded so I made the first split the string with the word
    > > <SPAN ID=TIME> then again split the string with the word </ SPAN>

    >
    > > works but is complicated:

    >
    > > $ string = "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>";
    > > @ part1 = split (/ <SPAN ID=TIME> /, $ string);
    > > @ part2 = split (/ <\ / SPAN> /, $ part1 [0]);
    > > print part2 [0];

    >
    > > I know that there is an easier way in Perl, but how?

    >


    $h = "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>";
    $h =~ / \<SPAN ID=TIME> (.*) \<\/ SPAN> /;
    print $1;

    12:30
     
    smallpond, Mar 9, 2011
    #4
  5. Re: how to extract the string part between the two marks

    "Beno" <> wrote:
    >Not to remove, I need to extract the tekst 12:30
    >
    >> how to remove the string part between the two marks
    >> for example
    >> from string "<DIV> </ DIV> <SPAN ID=TIME> 12:30 </ SPAN> <SOME TEXT>"
    >> extract the text "12:30"

    [...]
    >> I know that there is an easier way in Perl, but how?


    For a very simple-minded approach use

    if ($string =~ m+<SPAN ID=TIME> (.*) </SPAN>+) {
    print $1;
    }

    Of course this will fail as soon as the SPAN tag changes, even it is
    just some change in whitespace, or someone decided to enclose TIME in
    quotes, or changes SPAN to lower-case, or or or....

    The correct way is -as has been said a gazillion times in this NG- use
    an HTML parser to parse HTML and then simply retrieve the body of the
    SPAN tag.

    jue
     
    Jürgen Exner, Mar 10, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M. Clift

    String and quotation marks

    M. Clift, Oct 22, 2004, in forum: Python
    Replies:
    2
    Views:
    407
    M. Clift
    Oct 22, 2004
  2. GenxLogic
    Replies:
    3
    Views:
    1,370
    andrewmcdonagh
    Dec 6, 2006
  3. Vadim
    Replies:
    1
    Views:
    1,277
    Pascal J. Bourguignon
    Dec 24, 2008
  4. Peter Bunyan
    Replies:
    5
    Views:
    170
    Andrew Stewart
    Mar 20, 2008
  5. Anat
    Replies:
    2
    Views:
    242
    Lasse Reichstein Nielsen
    May 25, 2006
Loading...

Share This Page