453

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.

4 Answers 4

596
[^,;]+         

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:

[,;]+
5
  • 11
    And the question doesn't specify whether adjacent separators are allowed, so the trailing '+' is slightly dubious. Commented Sep 11, 2009 at 6:26
  • Getting an error only for semicolon-- unterminated regexp meets end of file
    – Jaswinder
    Commented Dec 21, 2017 at 3: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
    Commented Sep 4, 2018 at 15:01
  • @JonathanLeffler the trailing '+' gives you the exact match, check this demo if you remove it you will match every characters. Commented Jan 12 at 14:14
  • If anyone is curious how to restrict the above solution to only match on single lines (i.e. avoid matches that span multiple lines), you need to add \r\n to the character class: [^,;\r\n]+. Commented May 16 at 9:25
108

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

[^,;]
3
  • 3
    More about negated character classes
    – HEX
    Commented Oct 17, 2015 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
    Commented Sep 4, 2018 at 14:55
  • @Harry this regex works if you want to avoid the semicolon and comma at the end, check this demo. Commented Jan 12 at 18:01
56

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)

0
3

Use this:

([^,;]*[,;])*
3
  • 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. Commented Sep 11, 2009 at 6: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. Commented Sep 11, 2009 at 6: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
    Commented Sep 11, 2009 at 16:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.