In my web application, I want the user to enter only SELECT statements. I am using the following regex to validate the entry.
(^(?i)select.+)(\\w|\\W)
The above code makes sure the entry should start with SELECT and then a WHITESPACE
(Example: SELECT * FROM TABLE1)
But when the user have the ‘ENTER’ key while creating query, the regex fails.
So I want the REGEX to all ‘ENTER’ key. Can anyone please tell me the syntax for REGEX to allow the user to use ‘ENTER’ key in text area?
Small aside: Your code does nothing to ensure a whitespace after
SELECTbecause the dot matches any character except newlines.But now, to answer your question: You need the
(?s)flag to allow the dot to also match newlines. Plus, you should use a\stoken to assert the presence of whitespace. The trailing(\w|\W)is rather mysterious – it matches any character, too…So I would suggest this:
or even
because that’s already enough to check if the string starts with
SELECT<space>.