Regular expression ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Josip Maslaæ

    Regular expression ?

    Hello.

    I'm stuck! :)

    I need a regular expr. that would match any expresion that doesn't start
    with some words (not letters - words!)

    For example - reg. expr. that matches any expression that doesn't start with
    words 'one' and 'two' ??

    thanks !!






  • Shawn Wilson

    #2
    Re: Regular expression ?

    "Josip Maslaæ" wrote:[color=blue]
    >
    > I need a regular expr. that would match any expresion that doesn't start
    > with some words (not letters - words!)
    >
    > For example - reg. expr. that matches any expression that doesn't start with
    > words 'one' and 'two' ??[/color]

    if (!preg_match("/^(one|two)/", $foo)) {
    //do stuff
    }

    Shawn
    --
    Shawn Wilson
    shawn@glassgian t.com

    Comment

    • Paul 'piz' Wellner Bou

      #3
      Re: Regular expression ?

      Shawn Wilson wrote:[color=blue]
      > if (!preg_match("/^(one|two)/", $foo)) {
      > //do stuff
      > }[/color]

      I would do it like this, then you can put the words in an array.
      (extending Shawn's version a little bit)

      $wordlist = array("one","tw o","three");
      if(preg_match("/^(".join("|",$w ordlist).")/i", $foo)
      {
      // matched
      }

      greetz
      Paul.

      Comment

      • Josip Maslac

        #4
        Re: Regular expression ?

        "Shawn Wilson" <shawn@glassgia nt.com> wrote in message
        news:40311B25.4 E3A32A7@glassgi ant.com...[color=blue]
        > "Josip Maslaæ" wrote:[color=green]
        > >
        > > I need a regular expr. that would match any expresion that doesn't start
        > > with some words (not letters - words!)
        > >
        > > For example - reg. expr. that matches any expression that doesn't start[/color][/color]
        with[color=blue][color=green]
        > > words 'one' and 'two' ??[/color]
        >
        > if (!preg_match("/^(one|two)/", $foo)) {
        > //do stuff
        > }
        >[/color]

        Many thanks, but that is not really what I'm looking for.

        Like I said, I need just a reg. expression(!) that does what I need - not a
        work a round like this (solving it with !preg_match()).
        My question was just a part of the problem I need to solve, so this solution
        doesn't work for me.


        What I need is something like negating the class but on strings:
        for example:
        '/^[^ot]/' - matches all expressions that doesn't start with o or t,
        I need something like that but one whole string, not just letters.


        thanks


        Comment

        • Eric Bohlman

          #5
          Re: Regular expression ?

          "Josip Maslac" <josip.maslac@f er.hr> wrote in
          news:c0rcig$9cm $1@sunce.iskon. hr:
          [color=blue]
          > Like I said, I need just a reg. expression(!) that does what I need -
          > not a work a round like this (solving it with !preg_match()).
          > My question was just a part of the problem I need to solve, so this
          > solution doesn't work for me.
          >
          >
          > What I need is something like negating the class but on strings:
          > for example:
          > '/^[^ot]/' - matches all expressions that doesn't start with o
          > or t,
          > I need something like that but one whole string, not just letters.[/color]

          You could use a negative lookahead:

          /^(?!(one|two)\b )/

          Comment

          • Josip Maslaæ

            #6
            Re: Regular expression ?

            > You could use a negative lookahead:[color=blue]
            >
            > /^(?!(one|two)\b )/[/color]

            That's what I'm looking for !

            Thanks !


            Comment

            Working...