3

I have this regular expression:

^[a-zA-Z0-9]

I am trying to select any characters except digits or letters, but when I test this, only the first character is matched.

When I use

[a-zA-Z0-9]

the matches are correctly digits and letters. I need to negate it, but the ^ does not work.

Soviut
  • 88,194
  • 49
  • 192
  • 260
jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • `^` must be inside character group to negate, e.g. `[^a-zA-Z]` – marekful Jun 27 '17 at 02:21
  • Possible duplicate of [Negate characters in Regular Expression](https://stackoverflow.com/questions/1763071/negate-characters-in-regular-expression) – Soviut Jun 27 '17 at 02:22

3 Answers3

13

Below is a quick summary of regexps and how you can group together a query set using the commands below. In your case place the ^ inside the [a-zA-Z0-9] to achieve the desired result.

  .   Single character match except newline
  "." Anything in quotations marks literally
  A*  Zero or more occurrences of A
  A+  One or more occurrences of A
  A?  Zero or one occurrence of A
  A/B Match A only if followed by B
  ()  series of regexps grouped together
  []  Match any character in brackets
  [^] Do not match any character in brackets
  [-] Define range of characters to match
  ^   Match beginning of new line
  $   Match end of a line
  {}  Cardinality of pattern match set
  \   Escapes meta-characters
  |   Disjunctive matches, or operation match
Mobeen
  • 156
  • 7
5

Putting the ^ at start of your expression means "search from the beginning of the string". You need to put it inside the square brackets to make it a negation.

[^a-zA-Z0-9]
Soviut
  • 88,194
  • 49
  • 192
  • 260
3

To negate you must put the ^ inside the brackets:

[^a-zA-Z0-9]
Gerry
  • 10,337
  • 3
  • 31
  • 40