Menu

RegExp Search

Mirnava
2008-09-04
2012-11-13
  • Mirnava

    Mirnava - 2008-09-04

    In This example:
    ----------------------------------------------------------------------------------------------
    <tr><td>1</td><td>2</td><td>3</td><td>4</td></tr>
     
    <tr><td>I</td><td>        <p align="center"><strong>Cell</strong></p>
    </td><td></td><td></td></tr>
    ----------------------------------------------------------------------------------------------
    Regular expression: <td>[ ]*<p.+> searches upto fourth closing bracket (at the end of line)
    "<td>        <p align="center"><strong>Cell</strong></p>"
    ----------------------------------------------------------------------------------------------
    Regular expression: <td>[ ]*<p.+t  searches upto third letter "t" (second strong)
    ----------------------------------------------------------------------------------------------
    Regular expression: <td>[ ]*<p.+t  searches upto third letter "C" (in Cell)
    "<td>        <p align="center"><strong>C"
    ----------------------------------------------------------------------------------------------

    But I need select only two tags: "<td>        <p align="center">" for replace them.

    What I doing wrong?

     
    • Mirnava

      Mirnava - 2008-09-05

      Thank you very much, it worked.

      Expression:
      <td> *<p([^>]+)>
      replace by:
      <td\1>
      take me possibility to delete paragrath tags with no change cells formatting.

       
    • Fool4UAnyway

      Fool4UAnyway - 2008-09-04

      Suggested reading:

      "greedy and non-greedy reg ex" (Help forum)
      http://sourceforge.net/forum/forum.php?thread_id=1879138&forum_id=331754

       
    • Fool4UAnyway

      Fool4UAnyway - 2008-09-04

      > But I need select only two tags: "<td> <p align="center">"
      > for replace them.

      Well, let me help you right-away, because this seems pretty simple anyway.

      You want a two-tag match, the second of which can vary.
      You only want to match up to the end of the second tag.

      Let's translate this into: you don't want more than one > greater-than sign from the second tag.

      This is your magic regex:

      <td> *<p[^>]*>

      <td>_____ is simply exactly the first tag
      *_______ is any number of space characters, including none
      _________ if there is at least one space character, use " +"
      <p_______ match the opening of the second tag
      [^>]_____ match any character that is not the > greater-than
      *________ match it any number of times, including none at all
      >________ match the closing greater-than sign of the second tag

      This [^>] is called non-greedy, because it will finish matching as soon as you want it to. "." will match _any_ character, including the > greater-than sign.