Regex without \r\n and not entirely space
-
14-11-2019 - |
Question
I'm checking for valid user input for an executable; however it does include things like del
/rm
, dir
/ls
. The input is collected through XML and is validated using XSD. I will not check for file existence, since my program submits to a server, which may or may not have access to the same files.
The only requirements then, are that it not have a new line \r
or \n
and it cannot be entirely white space. I think it would be valid to assume that tab \t
would not be allowed either, but I am more concerned with newlines.
Thanks
Solution
Does this mean you have the limitations mentioned here: http://www.regular-expressions.info/xml.html
If so, then you probably want something like this:
[^\r\n\t]*[^\r\n\t\s][^\r\n\t]*
The middle part means there has to be one character that is not a newline, tab, or whitespace. The rest of it means zero or more characters around that character that aren't a newline or tab (but it can be whitespace). I think you might be able to remove the \r\n\t
from the middle group because they all might be encompassed in \s
but I haven't tested any of this.
Remove the three occurrences of \t
if you want tabs.
OTHER TIPS
I am not entirely sure what you want to do, but a regular expressions for "no newline and not just whitespace" would be
[ \t]*\S[^\r\n]*
This matches zero or more whitespace characters followed by a non-whitespace characters and an abitrary number of characters that are not \r or \n (including spaces and tabs). It cannot match a string consisting of only whitespace (as there would be no non-whitespace character matching \S).
To prohibit tabs also, you can change this to read
[ ]*\S[^\r\n\t]*