Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to matches anything except space and new line?

Tags:

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.

like image 596
Akashdeep Saluja Avatar asked Apr 23 '14 05:04

Akashdeep Saluja


People also ask

What is used to match anything except a whitespace?

You can match a space character with just the space character; [^ ] matches anything but a space character.

How do you match everything including newline regex?

The dot matches all except newlines (\r\n). So use \s\S, which will match ALL characters.

How do you match line breaks in regex?

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.

Does dot match new 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.


1 Answers

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 
like image 159
hwnd Avatar answered Sep 20 '22 13:09

hwnd