Regular Expressions help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Massimo at HOME

    Regular Expressions help!

    Probably, it is simple but honestly I am not able to solve this
    problem.

    I am trying to use eregi_replace() function to substitute some strings
    in my input.

    I would like to substitute the "ac" substring when it is a single word
    (i.e. "ac" or "ac " but not "ace" or "mace").

    I tried something like:

    ereg_replace("a c[^[:alpha:]]*","subst...",$ inputstring));

    But it fails...

    I read the regex help and online documentation but I was not able to
    figure how to obtain it...

    Please help!

    Thanks,
    Massimo
    --
    Documentazione sulla zecca di Parma e le sue monete e altri documenti sulla numismatica italiana

  • Andy Hassall

    #2
    Re: Regular Expressions help!

    On 13 Nov 2004 06:50:06 -0800, numismatica@gma il.com (Massimo at HOME) wrote:
    [color=blue]
    >Probably, it is simple but honestly I am not able to solve this
    >problem.
    >
    >I am trying to use eregi_replace() function to substitute some strings
    >in my input.
    >
    >I would like to substitute the "ac" substring when it is a single word
    >(i.e. "ac" or "ac " but not "ace" or "mace").
    >
    >I tried something like:
    >
    >ereg_replace(" ac[^[:alpha:]]*","subst...",$ inputstring));
    >
    >But it fails...
    >
    >I read the regex help and online documentation but I was not able to
    >figure how to obtain it...[/color]

    Don't use the ereg* functions, use the preg* functions. They're typically
    faster, and have a more powerful expression language (Perl-compatible).



    Perform a regular expression search and replace


    Have a look at \b - zero-width word boundary assertion.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Daniel Tryba

      #3
      Re: Regular Expressions help!

      Massimo at HOME <numismatica@gm ail.com> wrote:[color=blue]
      > I would like to substitute the "ac" substring when it is a single word
      > (i.e. "ac" or "ac " but not "ace" or "mace").
      >
      > I tried something like:
      >
      > ereg_replace("a c[^[:alpha:]]*","subst...",$ inputstring));
      >
      > But it fails...[/color]

      Why use ereg_replace?

      $ cat /tmp/foo.php
      <?php

      $str='foo ac mace bar';

      echo "before: $str\n";
      $str=preg_repla ce('/\bac\b/','xx',$str);
      echo "after : $str\n";
      ?>
      $ php4 /tmp/foo.php
      before: foo ac mace bar
      after : foo xx mace bar
      $

      Comment

      • Gary L. Burnore

        #4
        Re: Regular Expressions help!

        On 13 Nov 2004 15:15:02 GMT, Daniel Tryba <spam@tryba.inv alid> wrote:
        [color=blue]
        >Massimo at HOME <numismatica@gm ail.com> wrote:[color=green]
        >> I would like to substitute the "ac" substring when it is a single word
        >> (i.e. "ac" or "ac " but not "ace" or "mace").
        >>
        >> I tried something like:
        >>
        >> ereg_replace("a c[^[:alpha:]]*","subst...",$ inputstring));
        >>
        >> But it fails...[/color]
        >
        >Why use ereg_replace?[/color]

        Why ask why? He was asking for help. Why demean his question?

        [color=blue]
        >
        >$ cat /tmp/foo.php
        ><?php
        >
        >$str='foo ac mace bar';
        >
        >echo "before: $str\n";
        >$str=preg_repl ace('/\bac\b/','xx',$str);
        >echo "after : $str\n";
        >?>
        >$ php4 /tmp/foo.php
        >before: foo ac mace bar
        >after : foo xx mace bar
        >$[/color]

        Neat answer, but I liked the first and yet similar response. The
        poster wasn't a jerk and gave several URLs and a tip about \b so the
        person asking the question could learn for himself.


        --
        gburnore@databa six dot com
        ---------------------------------------------------------------------------
        How you look depends on where you go.
        ---------------------------------------------------------------------------
        Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
        | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
        DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
        | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
        Black Helicopter Repair Svcs Division | Official Proof of Purchase
        =============== =============== =============== =============== ===============
        Want one? GET one! http://signup.databasix.com
        =============== =============== =============== =============== ===============

        Comment

        • Daniel Tryba

          #5
          Re: Regular Expressions help!

          Gary L. Burnore <gburnore@datab asix.com> wrote:[color=blue]
          > Neat answer, but I liked the first and yet similar response. The
          > poster wasn't a jerk and gave several URLs and a tip about \b so the
          > person asking the question could learn for himself.[/color]

          Thank you for calling me a jerk.

          If you have nothing nice to say blabla...

          Comment

          • Gary L. Burnore

            #6
            Re: Regular Expressions help!

            On 13 Nov 2004 15:24:32 GMT, Daniel Tryba <spam@tryba.inv alid> wrote:
            [color=blue]
            >Gary L. Burnore <gburnore@datab asix.com> wrote:[color=green]
            >> Neat answer, but I liked the first and yet similar response. The
            >> poster wasn't a jerk and gave several URLs and a tip about \b so the
            >> person asking the question could learn for himself.[/color]
            >
            >Thank you for calling me a jerk.[/color]

            I said he wasn't a jerk. I didn't say you were. But then, if the shoe
            fits. I do note that you can't argue my point.
            --
            gburnore@databa six dot com
            ---------------------------------------------------------------------------
            How you look depends on where you go.
            ---------------------------------------------------------------------------
            Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
            Black Helicopter Repair Svcs Division | Official Proof of Purchase
            =============== =============== =============== =============== ===============
            Want one? GET one! http://signup.databasix.com
            =============== =============== =============== =============== ===============

            Comment

            • Daniel Tryba

              #7
              Re: Regular Expressions help!

              Gary L. Burnore <gburnore@datab asix.com> wrote:[color=blue][color=green][color=darkred]
              >>> Neat answer, but I liked the first and yet similar response. The
              >>> poster wasn't a jerk and gave several URLs and a tip about \b so the
              >>> person asking the question could learn for himself.[/color]
              >>
              >>Thank you for calling me a jerk.[/color]
              >
              > I said he wasn't a jerk. I didn't say you were. But then, if the shoe
              > fits. I do note that you can't argue my point.[/color]

              If I only could find out that your point was...

              Oh wait, I don't actually care.

              Comment

              • John Dunlop

                #8
                Re: Regular Expressions help!

                Gary L. Burnore wrote:
                [color=blue]
                > On 13 Nov 2004 15:15:02 GMT, Daniel Tryba <spam@tryba.inv alid> wrote:[/color]
                [color=blue][color=green]
                > > Why use ereg_replace?[/color]
                >
                > Why ask why?[/color]

                Why not?

                --
                Jock

                Comment

                • Massimo at HOME

                  #9
                  Re: Regular Expressions help!

                  Gary L. Burnore <gburnore@datab asix.com> wrote in message news:<cn58as$cj 5$2@blackhelico pter.databasix. com>...[color=blue]
                  > Neat answer, but I liked the first and yet similar response. The
                  > poster wasn't a jerk and gave several URLs and a tip about \b so the
                  > person asking the question could learn for himself.[/color]

                  I already learnead about \b and read the docs, but I was missing the /

                  Thanks,
                  Massimo
                  --
                  Documentazione sulla zecca di Parma e le sue monete e altri documenti sulla numismatica italiana

                  Comment

                  Working...