regex validation for keyboard special character- grails - regex

i am very new to this Grails.
I know there are ways in it to stop the input with specific character using constraints and matches for the field.
I am using it to stop the user from entering any special character from the keyboard
I have used
matches:/^[^$##*^%~]*$/
it checks that field does not have *^%$##~, and it works fine for this set of characters but I also want to restrict the user from adding +-(}/\|{[?]!<>~;',=&_.:" (in short all the special symbols on keyboard). And using only this constraints. I have tried putting them in this regular expression pattern but it is still allowing it or if does allow than it not showing in error message which were entered in the field.
For ex:- If I have entered (+)&^ than error message is shown only as "Please do not enter ^." but I want, "Please do not enter (+)&^."
Please let me know if anyone knows.
Please also note that I am required to use only Grails/Groovy support no JS/JQuery.
Thanks

Below regex will prevent from entering any character other than alphanumeric, and also at least one character. If you do not want minimum one character, then replace + with *
/^[^a-zA-Z0-9]+$/

Related

How to write a QRegExp that will accept only strings made of characters or normal symbols?

Let me clarify the question. I need to process keyboard input on the fly. So if the user presses something like the home button or the delete button I need to ignore the input. But any normal printable symbol I want to catch. I get the input in the text field() method of a QKeyEvent.
I've tried two regexps
The first one was:
QRegExp("[\\S]");
Which did not work as apparently pressing delete is considered a non-whitespace character.
The second one was
QRegExp("[\\w]");
This one was much better as it would only accept letters, number and underscore but no symbols such as + or *. Which I want to accept.
I was thinking of modifying the last expression to include any symbol in my keyboard. But how would I do that? Or how can I write an RegExp for what I want?

How to match a single Capital 'A' plus 8 digits long number with regex?

I need the user to a single letter 'A' plus 8 digits and no more. Eg. A00000000
Here is my code below kind of works but I want to limit the number of digits to 8. I am using MVC 4.
[RegularExpression("^[A][0-9]{8}$", ErrorMessage="Required format: A00000000")]
Although it does validate the user can keep typing forever. So there is no limit on the number of characters. It would be nice to stop the user from entering once it hits the maximum of 8 digits.
Thank you for reading.
It's not possible to limit the number of characters using regex alone, but you could use the maxlength attribute to specify the maximum number of characters allowed in your <input> element:
<input type="text" name="myInput" maxlength="9">
Unless you implement it yourself, a regex match can only operate on a string you have. And you won't have it before the user has typed it.
So if you want to prevent him from typing, you're going to have to do it before you get to the regex.
If you're trying to stop someone from typing more on something like say a textbox on a webpage, you would have to hook into the keypress event (keyup or keydown). Then once the validation fails, return "false" from the keypress event handler and it will stop the character from entering the textbox, or you may need to use preventDefault on the incoming event object (e.g. e.preventDefault()). If you're using jQuery, have a look at this webpage: https://api.jquery.com/keypress/ on how to set the event handler.

How to have multiple regex for same element

I have a text box which has a regular expression which is something like below
^AB[a-zA-Z0-9]{20}$
which basically allows charecter AB , followed by 20 either alphabetic or numbers, and for example lets consider the validation error for not following this regex is Some Test Error
I have a scenario where user enters AB1234 and tabs out of the text box, and the error Some Test Error shows, but I have a requirement of not showing the same error message Some Test Error if user is trying to follow the format but not adhering to the entire regex.
Scenario 1 :- User enters CD12345675438976524381
I need to show Some Test Error
Scenario 2 : USer enters AB12345
I need to shoe Different Test Error, because user tried to enter a value starting from AB*
How can achieve this, is there a way of specifying multiple regex's?
I am not sure which language you are using... but I suppose that you may change the regex, when user got the message once. While the user is trying to enter the entire string, don't count the number, unless the user input the 21st char or something not belong to [a-zA-Z0-9]...
I wish I made myself understood, the point is that I suppose you change the regex in time.
I think you can for example use multiple regexes and check the input:
if input is valid, everithing is ok,
if input is invalid check: a) if starts with AB (regex: ^AB) or if is valid length (regex ^([^A][^B][a-zA-Z0-9]{20})$) show proper info
if is totally invalid, give another info
OR you can use one long regex, like:
^(AB[a-zA-Z0-9]{20})$|^(AB[a-zA-Z0-9]{0,19}|AB[a-zA-Z0-9]{21,})$|^([^A][^B][a-zA-Z0-9]{20})$
DEMO
which capture given type of input in saparete groups,
and then find which groups was captured to check level of correctness:
if group 1 exist - valid string,
if group 2 - starts with AB but inproper length,
if group 3 - proper lenght, invalid beginning
I sure there are also other solutions.

EditPad: Need a regex that handles multiple possible data formats

First, I'm using EditPadPro for my regex cleaning, so any answers given should work within that environment.
I get a large spreadsheet full of data that I have to clean every day. I've managed to get it down to a couple of different regexes that I run, and this works... but I'm curious to see if it's possible to reduce down to a single regex.
Here is some sample data:
3-CPC_114851_70095_70095_CAN-bre
3-CPC_114851_70095_70095_CAN
b11-ao1-113775-bre
b7-ao-114441
b7-ao-114441-bre
b7-ao1-114441
b7-ao1-114441-bre
http://go.nlvid.com/results1/?http://bo
go.nlv/results1/?click
b4-sm-1359
b6-sm-1356-bre
1359_195_1453814569-bre
1356_104_1456856729
b15-rad-8905
b15-rad-8905-bre
Here is how the above data needs to end up:
114851-bre
114851
113775-bre
114441
114441-bre
114441
114441-bre
http://go.nlvid.com/results1/
go.nlv/results1/
sm-1359
sm-1356-bre
sm-1359-bre
sm-1356
rad-8905
rad-8905-bre
So, there are numerous rules, such as:
In cases of more than 2 underscores, the result needs to contain only the value immediately after the first underscore, and everything from the dash onwards.
In cases where the string contains "-ao-", "-ao1-", everything prior to the final numeric string should be removed.
If a question mark is present, everything from the mark onwards should be removed.
If the string contains "-sm-" or "-rad-", everything prior to those alpha strings should be removed.
If the string contains 2 underscores, averything after the first numeric string up to a dash
(if present) should be removed, and the string "sm-" should be prepended.
Additionally there is other data that must be left untouched, including but not limited to:
113535|24905|24905
as well as many variations on this pattern of xxxxxx|yyyyy|zzzzz (and not always those string lengths)
This may be asking way too much of regex, I'm not sure as I'm not great with it. But I've seen some pretty impressive things done with it, so I thought I'd put this out to the community and see what you come back with.
Jonathan, I can wrap all of those into one regex, except the last one (where you prepend sm- to a string that does not contain sm). It is not possible in this context, because we cannot capture "sm" to reuse in the replacement, and because there is no "conditional replacement" syntax in EPP.
That being said, you can achieve what you want in EPP with two regexes and one macro to chain the two.
Here is how.
The solution below is tested in EPP.
Regex 1
Press Ctrl + Sh + F to enter Search / Replace mode
Enter the following Search and Replace in the appropriate boxes
At the top right of the Search bar, click the Favorite Searches pull-down, select "Add", give it a name, e.g. Regex 1
Search:
(?mx)^
(?=(?:[^_\r\n]*?_){3})[^_\r\n]+?_([^_\r\n]+)[^-\r\n]+(-[^\r\n]+)?
|
[^\r\n]*?-ao1?-\D*([^\r\n]+)
|
([^\r\n?]*)(?=\?)[^\r\n]+
|
[^\r\n]*?-((?:sm|rad)-[^\r\n]+)
Replace:
\1\2\3\4\5
Regex 2
Same 1-2-3 steps as above.
Search
^(?!(?:[^_\r\n]*?_){3})(?=(?:[^_\r\n]*?_){2})(\d+)(?:[^-\r\n]+(-[^\r\n]+)?)
Replace
sm-\1\2
Chaining Regex 1 and Regex 2
Top menu: Macros, Record Macro, give it a name.
Click the Favorite searches pulldown, select Regex 1
Hit Replace All.
Click the Favorite searches pulldown, select Regex 2
Hit Replace All.
Macros, Stop recording.
Whenever you want to do your sequence of replacements, pull it by name under the Macros menu.
Testing This
I have tested my "Jonathan macro" on your input. Here is the result:
114851-bre
114851
113775-bre
114441
114441-bre
114441
114441-bre
http://go.nlvid.com/results1/
go.nlv/results1/
sm-1359
sm-1356-bre
sm-1359-bre
sm-1356
rad-8905
rad-8905-bre
Try this:
Toggle the Search Panel : SHIFT+CTRL+F
SEARCH: .*?((?:sm-|rad-)?(?:(?:\d+|[\w\.]+\/.*?))(?:-\w+)?$)
REPLACE: $1
Check REGEX and WORDS
Click Replace All or Hit CTRL+ALT+F3
Check the image below:

How can I use gvim to add a carriage return (aka ENTER) to a pattern?

What's the vi/gvim syntax to replace a pattern with a pattern that includes <ENTER>? I know this is possible but never felt like diving too deep in the documentation to know how to do it.
Something like this:
:s/\(word\)/\1<ENTER>/
But correctly :)
Thanks
Use the "escape" encoding:
:s/\(word\)/\1\r/
See the Vim documentation for pattern whitespace escapes.
:s/\(word\)/\1\r/
Alternatively, use Ctrl+V or Ctrl+Q to quote (escape) the Enter key:
:s/\(word\)\1^QENTER/
Where ^Q is Ctrl+Q and ENTER is the Enter key.
Clarification: Depending on your installation, either ^Q or ^V should work. The quoting character differs on some platforms.
(This has the helpful side-effect of inserting the appropriate end-of-line character for whichever platform you're using, eliminating the CR vs. LF vs. CRLF problem.)
Just for clarification purposes, now that we're talking about carriage return, it should be noted that RETURN and ENTER key are not the same, or it would be more correct to say, they should not be the same.
I haven't used a desktop keyboard for some time now, but the ENTER key is usually the one on the down right side, while the RETURN key is the big one in the middle.
RETURN key is the one that should be used for entering a carriage return, while ENTER key is the one that should be used for entering commands. I remember an old DOS editor EDT, in which RETURN key was for newline and ENTER key was for giving commands. You couldn't give a command with RETURN. I think ENTER also gave ^1 (line feed).
Today that difference is somewhat lost, although I still, now and then, run into an editor that respects it.
2 examples:
One, two and an even more obvious three