447

Is it possible to define a regex which will match every character except a certain defined character or set of characters?

Basically, I wanted to split a string by either comma (,) or semi-colon (;). So I was thinking of doing it with a regex which would match everything until it encountered a comma or a semi-colon.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
KJ Saxena
  • 21,452
  • 24
  • 81
  • 109

4 Answers4

589
[^,;]+         

You haven't specified the regex implementation you are using. Most of them have a Split method that takes delimiters and split by them. You might want to use that one with a "normal" (without ^) character class:

[,;]+
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • 11
    And the question doesn't specify whether adjacent separators are allowed, so the trailing '+' is slightly dubious. – Jonathan Leffler Sep 11 '09 at 06:26
  • Getting an error only for semicolon-- unterminated regexp meets end of file – Jaswinder Dec 21 '17 at 03:07
  • I had a similar requirement where I want to avoid semicolon and comma at the end I tried a lot but no success below is the Regex I am using const regexDomain = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9]/g; Well it validates if I use , and ; in between but fails at the end to vliadate. – Harry Sep 04 '18 at 15:01
106

Use character classes. A character class beginning with caret will match anything not in the class.

[^,;]
Thom Smith
  • 13,916
  • 6
  • 45
  • 91
  • 3
    More about [negated character classes](http://www.regular-expressions.info/charclass.html) – HEX Oct 17 '15 at 21:09
  • I had a similar requirement where I want to avoid semicolon and comma at the end I tried a lot but no success below is the Regex I am using const regexDomain = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9]/g; Well it validates if I use , and ; in between but fails at the end to vliadate. – Harry Sep 04 '18 at 14:55
52

Use a negative character class:

[^,;]+

This will match at least one character that is not a comma nor a semicolon. If there are multiple characters matching this criterion, all of them will be matched (+ at-least-once quantifier)

knittl
  • 246,190
  • 53
  • 318
  • 364
2

Use this:

([^,;]*[,;])*
NawaMan
  • 901
  • 5
  • 12
  • 5
    That requires the comma or semi-colon as a field delimiter, rather than as a field separator. The difference matters at the end of a 'line' (or other scanned record structure); typically, you don't want to insist on a comma or semi-colon after the last field. If your regex engine is powerful enough, you can use '`(?:([^,;]*)(?:[^,;]|$))`' (PCRE with non-capturing parentheses). The alternatives of a comma or semi-colon after the field, or end of record, makes things work better. Also consider whether empty fields are allowed. – Jonathan Leffler Sep 11 '09 at 06:24
  • 1
    Finally, you have to worry about what is actually returned by the captures - did you really want the separators included, and if there are 10 fields on a line, how many of them are returned by the capture notation. – Jonathan Leffler Sep 11 '09 at 06:25
  • 1
    You are right about all that but the reason I didn't concert those thing in my answer is that I do not know what language/library of RegEx the questioner is asking. He may be using "GREP". Anyway, I appreciate you adding those comments to clear things up for him. :D – NawaMan Sep 11 '09 at 16:48