Loading ...
Sorry, an error occurred while loading the content.

Extending classes in regexps

Expand Messages
  • Kubicek, David
    Hello, I m trying to find out how to `extend matching range of those predefined character-matching classes. For example, to use w and add to it a full stop
    Message 1 of 2 , Nov 25, 2004
    • 0 Attachment
      Hello,

      I'm trying to find out how to `extend matching range' of those predefined
      character-matching classes. For example, to use "\w" and add to it a full
      stop mark; e.g. to match the string "hello_world.txt" using \w (to make the
      regexp AS SHORT AS POSSIBLE), I'd imagine something like this should work:
      [\w\.]*
      but it doesn't. When used "[[:alnum:]_\.]*", though, it works. What I'm
      trying to achieve are the shortest possible regexps and using predefined
      classes is obviously the best way to do so. Any idea how to use classes this
      way?

      TIA,

      --
      David Kubíček,
      systémový specialista

      gedas ČR s.r.o.
      TGM 840, Mladá Boleslav
      Phone: +420-(326)-711 408
      Fax: +420-(326)-711 420
      Cellphone: +420-(736)-738 619
    • Mathias Michaelis
      Hi David ... But it might not work as you like to ;-) The reason is that within [] is _not_ a special character. So [ w .] finds and w and again and a
      Message 2 of 2 , Nov 25, 2004
      • 0 Attachment
        Hi David

        > I'd imagine something like this should work: [\w\.]* but it
        > doesn't. When used "[[:alnum:]_\.]*", though, it works.
        >
        But it might not work as you like to ;-) The reason is that \ within
        [] is _not_ a special character. So [\w\.] finds \ and w and \ again
        and a point `.´ (because a point isn't a special char as well). The
        same is true for [[:alnum:]_\.]. This also matches hello\word.txt.

        So you have to use [[:alnum:]_.]\+ or \(\w\|\.\)\+. You can also use
        the option \v ("very magic") and then write \v(\w|\.)+.

        Note that [:alnum:] finds also special chars as ç, é, ö etc.,
        whereas \w does not.

        Not also that you have to use \+ instead of * because a* matches
        zero or more `a´, but you probably want to match one or more `a´.

        Regards
        Mathias
      Your message has been successfully submitted and would be delivered to recipients shortly.