448

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

590
[^,;]+         

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:

[,;]+
3
  • 11
    And the question doesn't specify whether adjacent separators are allowed, so the trailing '+' is slightly dubious. Sep 11, 2009 at 6:26
  • Getting an error only for semicolon-- unterminated regexp meets end of file
    – Jaswinder
    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
    Sep 4, 2018 at 15:01
106

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

[^,;]
2
  • 3
    More about negated character classes
    – HEX
    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
    Sep 4, 2018 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)

0
2

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. 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. 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
    Sep 11, 2009 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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