regular expression help

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

    regular expression help

    Hi all,

    given a string:

    " <img class='myclass' alt='my picture' src='image.jpg' /"

    (where alt and src are changeable)

    how do I create the following string:

    " <div class='containe r'>
    <div class='inner'>
    <img class='myclass' alt='my picture' src='image.jpg' />
    <p>my picture</p>
    </div>
    </div"

    (where the contents of <p></pare the same as the image 'alt') ?


    I believe a regular expression would solve this but I'm not sure on the
    syntax.

    something like:

    regex.replace(" <img class='myclass' alt='$1' src='$2' />","<div
    class='containe r'><div class='inner'>< img class='myclass' alt='$1'
    src='$2' /><p>$1</p></div></div>")


    Any ideas? Thanks!

    --

    fiddlewidawiddu m
  • Alexey Smirnov

    #2
    Re: regular expression help

    On Aug 2, 8:16 pm, Stimp <r...@spumco.co mwrote:
    Hi all,
    >
    given a string:
    >
    " <img class='myclass' alt='my picture' src='image.jpg' /"
    >
    (where alt and src are changeable)
    >
    how do I create the following string:
    >
    " <div class='containe r'>
        <div class='inner'>
          <img class='myclass' alt='my picture' src='image.jpg' />
          <p>my picture</p>
        </div>
      </div"
    >
    (where the contents of <p></pare the same as the image 'alt')  ?
    >
    I believe a regular expression would solve this but I'm not sure on the
    syntax.
    >
    something like:
    >
    regex.replace(" <img class='myclass' alt='$1' src='$2' />","<div
    class='containe r'><div class='inner'>< img class='myclass' alt='$1'
    src='$2' /><p>$1</p></div></div>")
    >
    Any ideas? Thanks!
    >
    --
    >
    fiddlewidawiddu m
    try something like this one

    regex.replace(
    @"\<img\sclass\ ='myclass'\salt \='(.*?)'\ssrc\ ='(.*?)'\s\/\>",
    @"<div class='containe r'><div class='inner'>< img class='myclass'
    alt='$1' src='$2' /><p>$1</p></div></div>");

    if you need a line breaks in the final string, then add \n


    Comment

    • Stimp

      #3
      Re: regular expression help

      On Sat, 2 Aug 2008 Alexey Smirnov <alexey.smirnov @gmail.comwrote :
      On Aug 2, 8:16 pm, Stimp <r...@spumco.co mwrote:
      >
      try something like this one
      >
      regex.replace(
      @"\<img\sclass\ ='myclass'\salt \='(.*?)'\ssrc\ ='(.*?)'\s\/\>",
      @"<div class='containe r'><div class='inner'>< img class='myclass'
      alt='$1' src='$2' /><p>$1</p></div></div>");
      >
      if you need a line breaks in the final string, then add \n
      cheers I'll give it a shot now!

      --

      fiddlewidawiddu m

      Comment

      Working...