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.
Related
I have the following text and i would like to grab the keyword "ATTEMPT TO ACCESS DATABASE" and "was denied", and combine these two together. However, there are some user and path name information in between these two keywords, and this makes it difficult for me to capture the thing i want, as i'm still not yet a regex expert. XD
<182>Mar 27 09:38:55 4.3.2.1 [5439570:00311-46004] 03/11/2015 14:13:05
ATTEMPT TO ACCESS DATABASE mail/abc.nsf by USER was denied
Is there a single regex expression that can help me to fulfill my requirement? Would greatly appreciate for all the help!
Have a look at the following regex:
(ATTEMPT\s+TO\s+ACCESS\s+DATABASE\s+)(\S+)\s+by\s+([\w.-]+)\s+(was\s+denied)
Ouput is:
MATCH 1
1. [71-98] `ATTEMPT TO ACCESS DATABASE `
2. [98-110] `mail/abc.nsf`
3. [114-118] `USER`
4. [119-129] `was denied`
So, you can combine Group 1 and 4 to get ATTEMPT TO ACCESS DATABASE was denied (in any case due to i option), and Group 2 will contain the path name, and Group 3 will hold the user name.
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:
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]+$/
I'm in great trouble.
I must check if a string fits (matches) another string with RegEx.
For example, given the following string:
Apr 2 13:42:32 sandbox izxp[12000]: Received disconnect from 10.11.106.14: 10: disconnected by user
In the editable input field I give the program the following shortened string:
Received disconnect from 10.11.106.14: 10
If it fits the existing string (as you can see above), it is OK.
If any part of the new edited string doesn't fit the original string, I must warn the user with a message.
Could you help me solving this question with RegEx? Or another method?
I would appreciate it!
You must get the original string in a variable, let's call it $original (this is perl). Then you must get the input from the "editable input field", let's call it $input.
Then it is a simple
if ($original=~/$input/)
{
#Your code for a message to the user here
}
Your solution would be less regex and more escaping. Assuming you're going to use no regex patterns and just search for the input string literal, you should write your function so that it turns this
Received disconnect from 10.11.106.14: 10
into this
Received disconnect from 10\.11\.106\.14: 10
This can be achieved with many different libraries depending on which language you are using.
That will then allow you to check for a match.
Regular Expressions are more designed for common patterns in strings, rather than finding exact literals.
I have a CSV file which has been generated by a system. The problem is with one of the fields which used to be a list of items. An example of the original list is below....
The serial number of the desk is 45TYTU
This is the second item in the list
The colour of the apple is green
The ID code is 489RUI
This is the fourth item in the list.
And unfortunately the system spits out the code below.....
The serial number of the desk is 45TYTUThis is the second item in the listThe colour of the apple is greenThe ID code is 489RUIThis is the fourth item in the list.
As you can see, it ignores the line breaks and just bunches everything up. I am unable to modify the system that generates this output so what I am trying to do is come up with some sort of regex find and replace expression that will separate them out.
My original though would be to try and detect when an upper case letter is in the middle of a lower case word, but as in one of the items in the example, when a serial number is used it throws this out.
Anyone any suggestions? Is regex the way to go?
--- EDIT ---
I think i need to simplify things for myself, if I ignore the fact that lines that end in a serial number will break things for now. I need to just create an expression that will insert a line break if it detects that an upper case letter is being used after a lower case one
--- EDIT 2 ---
Using the example given by fardjad everything works for the sample data given, the strong was...
(.(?=[A-Z][a-z]))
Now as I test with more data I can see an issue appearing, certain lines begin with numbers so it is seeing these as serial numbers, you can see an example of this at http://regexr.com?2vfi5
There are only about 10 known numbers it uses at the start of the lines such as 240v, 120v etc...
Is there a way to exclude these?
That won't be a robust solution but this is what you asked. It matches the character before an uppercase letter followed by a lowercase one. You can simply use regex replace and append a new line character:
(.(?=[A-Z][a-z]))
see this demo.
You could search for this
(?<=\p{Ll})(?=\p{Lu})
and replace with a linebreak. The regex matches the empty space between a lowercase letter \p{Ll} and an uppercase letter \p{Lu}.
This assumes you're using a Unicode-aware regex engine (.NET, PCRE, Perl for example). If not, you might also get away with
(?<=[a-z])(?=[A-Z])
but this of course only detects lower-/uppercase changes in ASCII words.