help with regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Petra Meier

    help with regex

    Hello,

    I use the following script to parse URI and email:

    function parseLinks($sDa ta){
    $regexEmail = "/\w+((-\w+)|(\.\w+))*\ @[A-Za-z0-9]+((\.|-)[A-
    Za-
    z0-9]+)*\.[A-Za-z0-9]+/";
    $sData = preg_replace($r egexEmail, "<a id='external'
    href='mailto:'$ 0'>$0</a>", $sData);
    $regexURI = '#(^|[^\"=]{1})(http://|ftp://|https://|news:)([^
    \s<>]+)
    ([\s\n<>]|$)#sm';
    $sData = preg_replace($r egexURI, "\\1<a id=\"external\" name=
    \"\\2\
    \3\">\\2\\3</a>\\4", $sData);
    return $sData;

    }

    it works just fine, unfortunately I parse the links from texts where I
    have fullstops, commas, colons and semicolos at the end of URI which
    are parsed as part of the URI:

    A sentence with a link http://uri.in.senctence.com/foo.html, and a
    comma after the link

    would be parsed to:

    A sentence with a link <a href="http://uri.in.senctenc e.com/
    foo.html,">http ://uri.in.senctenc e.com/foo.html,<a/and a comma after
    the link

    I want to have it like this though:

    A sentence with a link <a href="http://uri.in.senctenc e.com/
    foo.html">http://uri.in.senctenc e.com/foo.html<a/>, and a comma after
    the link

    I already invested a lot of time with these regex and didn't succeed.
    Any of you regex-gurus could help me please! :)

    Petra Meier

  • Petra Meier

    #2
    Re: help with regex

    noone has an idea? :(

    On Mar 5, 4:57 pm, "Petra Meier" <mord...@sms.at wrote:
    Hello,
    >
    I use the following script to parse URI and email:
    >
    function parseLinks($sDa ta){
    $regexEmail = "/\w+((-\w+)|(\.\w+))*\ @[A-Za-z0-9]+((\.|-)[A-
    Za-
    z0-9]+)*\.[A-Za-z0-9]+/";
    $sData = preg_replace($r egexEmail, "<a id='external'
    href='mailto:'$ 0'>$0</a>", $sData);
    $regexURI = '#(^|[^\"=]{1})(http://|ftp://|https://|news:)([^
    \s<>]+)
    ([\s\n<>]|$)#sm';
    $sData = preg_replace($r egexURI, "\\1<a id=\"external\" name=
    \"\\2\
    \3\">\\2\\3</a>\\4", $sData);
    return $sData;
    >
    }
    >
    it works just fine, unfortunately I parse the links from texts where I
    have fullstops, commas, colons and semicolos at the end of URI which
    are parsed as part of the URI:
    >
    A sentence with a linkhttp://uri.in.senctenc e.com/foo.html, and a
    comma after the link
    >
    would be parsed to:
    >
    A sentence with a link <a href="http://uri.in.senctenc e.com/
    foo.html,">http ://uri.in.senctenc e.com/foo.html,<a/and a comma after
    the link
    >
    I want to have it like this though:
    >
    A sentence with a link <a href="http://uri.in.senctenc e.com/
    foo.html">http://uri.in.senctenc e.com/foo.html<a/>, and a comma after
    the link
    >
    I already invested a lot of time with these regex and didn't succeed.
    Any of you regex-gurus could help me please! :)
    >
    Petra Meier

    Comment

    • Alan

      #3
      Re: help with regex


      "Petra Meier" <mordret@sms.at wrote in message
      news:1173258231 .245528.24030@6 4g2000cwx.googl egroups.com...
      noone has an idea? :(
      >
      On Mar 5, 4:57 pm, "Petra Meier" <mord...@sms.at wrote:
      >Hello,
      >>
      >I use the following script to parse URI and email:
      >>
      <snip>
      > $regexURI = '#(^|[^\"=]{1})(http://|ftp://|https://|news:)([^
      >\s<>]+)
      >([\s\n<>]|$)#sm';
      <snip>

      >>
      >I want to have it like this though:
      >>
      > A sentence with a link <a href="http://uri.in.senctenc e.com/
      >foo.html">http ://uri.in.senctenc e.com/foo.html<a/>, and a comma after
      >the link
      >>
      >I already invested a lot of time with these regex and didn't succeed.
      >Any of you regex-gurus could help me please! :)
      >>
      >Petra Meier
      >
      >
      Petra,
      Not a regex-expert, and not absolutely clear from your description exactly
      what you want, but for what its worth and as you have had no other reply,
      perhaps:-

      $regexURI =
      '#(^|(?<=>))(ht tp://|ftp://|https://|news:)([^\s]+(htm)l?)?([\.,;:]?|$)#sm';

      $repString = "$1<a id=\"external\" name=\"$2$3\">$ 2$3</a>$5";

      --
      Alan


      Comment

      • Rik

        #4
        Re: help with regex

        Alan <al@spamless.ne twrote:
        >>I want to have it like this though:
        >>>
        >> A sentence with a link <a href="http://uri.in.senctenc e.com/
        >>foo.html">htt p://uri.in.senctenc e.com/foo.html<a/>, and a comma after
        >>the link
        >>>
        >>I already invested a lot of time with these regex and didn't succeed..
        >>Any of you regex-gurus could help me please! :)
        >>>
        >>Petra Meier
        >>
        >>
        Petra,
        Not a regex-expert, and not absolutely clear from your description
        exactly
        what you want, but for what its worth and as you have had no other reply,
        perhaps:-
        Well, the reason there's no reply is dead simple: comma's are valid in
        urls. Dots are valid in urls, etc. There is no real simple way to do it.


        --
        Rik Wasmus
        Posted on Usenet, not any forum you might see this in.
        Ask Smart Questions: http://tinyurl.com/anel

        Comment

        • try@anotheraddress.com

          #5
          Re: help with regex


          I think your problem is you dont really know what regex is or is for
          this is not a regex problem but a programming one.

          Comment

          • Rik

            #6
            Re: help with regex

            <try@anotheradd ress.comwrote:
            I think your problem
            Well, the OP's problem...
            is you dont really know what regex is or is for
            this is not a regex problem but a programming one.
            Hardly. If it's unalterable data, and somehow url's have to be matched,
            it's pretty much a regex job.

            Now, if this was about new content added, it would be a
            UI-related/scripting problem.

            --
            Rik Wasmus
            Posted on Usenet, not any forum you might see this in.
            Ask Smart Questions: http://tinyurl.com/anel

            Comment

            Working...