I have a string, I just want to match string for any character except for space and new line. What must be regular expression for this?
I know regular expressions for anything but space i.e. [^ ]+
and regular expression for anything but new line [^\n]+
(I'm on Windows). I am not able to figure how to club them together.
You can match a space character with just the space character; [^ ] matches anything but a space character.
The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.
If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match. Line breaks can be useful “anchors” that define where some pattern occurs in relation to the beginning or end of a line.
By default in most regex engines, . doesn't match newline characters, so the matching stops at the end of each logical line. If you want . to match really everything, including newlines, you need to enable "dot-matches-all" mode in your regex engine of choice (for example, add re. DOTALL flag in Python, or /s in PCRE.
You can add the space character to your character class to be excluded.
^[^\n ]*$
Regular expression
^ # the beginning of the string [^\n ]* # any character except: '\n' (newline), ' ' (0 or more times) $ # before an optional \n, and the end of the string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With