regexp help please

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

    #1

    regexp help please

    Hi,

    I think I am need of a regular expression with regards to a str_replace
    need that I have...

    I have a string like this one...

    "text text text{
    text
    } text {text text } t {text} txt { text }"

    I need to replace all occurances of "{" with "{ " and "}" with " }" where
    the following rules apply...

    replace "{" with "{ " when the character to the right of "{" is NOT
    <newline> and is NOT <space> and is NOT <tab>.

    replace "}" with " }" when the character to the left of "}" is NOT
    <newline> and is NOT <space> and is NOT <tab>.

    So, the result in my example above would be....

    "text text text{
    text
    } text { text text } t { text } txt { text }"

    Would anyone possibly know how to do this?

    Thanks

    --
    most midwives do not have children
  • Justin Koivisto

    #2
    Re: regexp help please

    Disco Octopus wrote:[color=blue]
    > Hi,
    >
    > I think I am need of a regular expression with regards to a str_replace
    > need that I have...
    >
    > I have a string like this one...
    >
    > "text text text{
    > text
    > } text {text text } t {text} txt { text }"
    >
    > I need to replace all occurances of "{" with "{ " and "}" with " }" where
    > the following rules apply...
    >
    > replace "{" with "{ " when the character to the right of "{" is NOT
    > <newline> and is NOT <space> and is NOT <tab>.
    >
    > replace "}" with " }" when the character to the left of "}" is NOT
    > <newline> and is NOT <space> and is NOT <tab>.
    >
    > So, the result in my example above would be....
    >
    > "text text text{
    > text
    > } text { text text } t { text } txt { text }"
    >
    > Would anyone possibly know how to do this?[/color]

    You would need to use both a zero-width negative look-ahead and a
    zero-width negative look-behind with prel style regex...

    <?php
    $s =<<<EOS
    text text text{
    text
    } text {text text } t {text} txt { text }
    EOS;

    $search1='`\{(? ![\s\r\n\t])`';
    $replace1='{ ';
    $s=preg_replace ($search1,$repl ace1,$s);

    $search2='`(?<![\s\r\n\t])\}`';
    $replace2=' }';
    $s=preg_replace ($search2,$repl ace2,$s);

    echo $s;

    ?>


    --
    Justin Koivisto, ZCE - justin@koivi.co m

    Comment

    • Chung Leong

      #3
      Re: regexp help please


      Justin Koivisto wrote:[color=blue]
      > Disco Octopus wrote:[color=green]
      > > Hi,
      > >
      > > I think I am need of a regular expression with regards to a str_replace
      > > need that I have...
      > >
      > > I have a string like this one...
      > >
      > > "text text text{
      > > text
      > > } text {text text } t {text} txt { text }"
      > >
      > > I need to replace all occurances of "{" with "{ " and "}" with " }" where
      > > the following rules apply...
      > >
      > > replace "{" with "{ " when the character to the right of "{" is NOT
      > > <newline> and is NOT <space> and is NOT <tab>.
      > >
      > > replace "}" with " }" when the character to the left of "}" is NOT
      > > <newline> and is NOT <space> and is NOT <tab>.
      > >
      > > So, the result in my example above would be....
      > >
      > > "text text text{
      > > text
      > > } text { text text } t { text } txt { text }"
      > >
      > > Would anyone possibly know how to do this?[/color]
      >
      > You would need to use both a zero-width negative look-ahead and a
      > zero-width negative look-behind with prel style regex...
      >
      > <?php
      > $s =<<<EOS
      > text text text{
      > text
      > } text {text text } t {text} txt { text }
      > EOS;
      >
      > $search1='`\{(? ![\s\r\n\t])`';
      > $replace1='{ ';
      > $s=preg_replace ($search1,$repl ace1,$s);
      >
      > $search2='`(?<![\s\r\n\t])\}`';
      > $replace2=' }';
      > $s=preg_replace ($search2,$repl ace2,$s);
      >
      > echo $s;
      >
      > ?>
      >
      >
      > --
      > Justin Koivisto, ZCE - justin@koivi.co m
      > http://koivi.com[/color]

      \r, \n, \t are covered by \s already, so there is no need to specify
      them separately. The code can be further compacted by using array
      arguments to preg_replace():

      $search = array('`\{(?!\s )`', '`(?<!\s)\}`');
      $replace= array('{ ', ' }');
      $s=preg_replace ($search,$repla ce,$s);

      Comment

      • Justin Koivisto

        #4
        Re: regexp help please

        Chung Leong wrote:[color=blue]
        > Justin Koivisto wrote:[color=green]
        >> Disco Octopus wrote:[color=darkred]
        >>> Hi,
        >>>
        >>> I think I am need of a regular expression with regards to a str_replace
        >>> need that I have...
        >>>
        >>> I have a string like this one...
        >>>
        >>> "text text text{
        >>> text
        >>> } text {text text } t {text} txt { text }"
        >>>
        >>> I need to replace all occurances of "{" with "{ " and "}" with " }" where
        >>> the following rules apply...
        >>>
        >>> replace "{" with "{ " when the character to the right of "{" is NOT
        >>> <newline> and is NOT <space> and is NOT <tab>.
        >>>
        >>> replace "}" with " }" when the character to the left of "}" is NOT
        >>> <newline> and is NOT <space> and is NOT <tab>.
        >>>
        >>> So, the result in my example above would be....
        >>>
        >>> "text text text{
        >>> text
        >>> } text { text text } t { text } txt { text }"
        >>>
        >>> Would anyone possibly know how to do this?[/color]
        >> You would need to use both a zero-width negative look-ahead and a
        >> zero-width negative look-behind with prel style regex...
        >>
        >> <?php
        >> $s =<<<EOS
        >> text text text{
        >> text
        >> } text {text text } t {text} txt { text }
        >> EOS;
        >>
        >> $search1='`\{(? ![\s\r\n\t])`';
        >> $replace1='{ ';
        >> $s=preg_replace ($search1,$repl ace1,$s);
        >>
        >> $search2='`(?<![\s\r\n\t])\}`';
        >> $replace2=' }';
        >> $s=preg_replace ($search2,$repl ace2,$s);
        >>
        >> echo $s;
        >> ?>
        >>[/color]
        > \r, \n, \t are covered by \s already, so there is no need to specify
        > them separately.[/color]

        That's what i thought, but I was too lazy to look it up. ;)
        [color=blue]
        > The code can be further compacted by using array
        > arguments to preg_replace():
        >
        > $search = array('`\{(?!\s )`', '`(?<!\s)\}`');
        > $replace= array('{ ', ' }');
        > $s=preg_replace ($search,$repla ce,$s);[/color]

        That is how I tested it, but then I pulled it apart for clarity of what
        was going on.

        --
        Justin Koivisto, ZCE - justin@koivi.co m

        Comment

        • Disco Octopus

          #5
          Re: regexp help please

          Chung Leong wrote:[color=blue]
          > Justin Koivisto wrote:[color=green]
          >> Disco Octopus wrote:[color=darkred]
          >>> Hi,
          >>>
          >>> I think I am need of a regular expression with regards to a str_replace
          >>> need that I have...
          >>>[/color][/color][/color]

          <....>
          [color=blue][color=green]
          >>
          >> You would need to use both a zero-width negative look-ahead and a
          >> zero-width negative look-behind with prel style regex...
          >>
          >> <?php[/color][/color]

          <great stuff>
          [color=blue][color=green]
          >> ?>[/color][/color]
          [color=blue]
          >
          > \r, \n, \t are covered by \s already, so there is no need to specify[/color]

          <more great stuff>
          [color=blue]
          >
          > $search = array('`\{(?!\s )`', '`(?<!\s)\}`');
          > $replace= array('{ ', ' }');
          > $s=preg_replace ($search,$repla ce,$s);[/color]

          Thanks very much Juston and Chung. This works like a charm for me, and is
          hugely appreciated.

          --
          if it itches, it will be scratched

          Comment

          Working...