Regular Expression To exclude sub-string name(job corps) Includes at least 1 upper case letter, 1 lower case letter, 1 number and 1 symbol except "#" - asp.net

Regular Expression To exclude sub-string name(job corps)
Includes at least 1 upper case letter, 1 lower case letter, 1 number and 1 symbol except "#"
I have written something like below :
^((?!job corps).)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[!#$%^&*]).*$
I tested with the above regular expression, not working for special character.
can anyone guide on this..

If I understand well your requirements, you can use this pattern:
^(?![^a-z]*$|[^A-Z]*$|[^0-9]*$|[^!#$%^&*]*$|.*?job corps)[^#]*$
If you only want to allow characters from [a-zA-Z0-9^#$%&*] changes the pattern to:
^(?![^a-z]*$|[^A-Z]*$|[^0-9]*$|[^!#$%^&*]*$|.*?job corps)[a-zA-Z0-9^#$%&*]*$
details:
^ # start of the string
(?! # not followed by any of these cases
[^a-z]*$ # non lowercase letters until the end
|
[^A-Z]*$ # non uppercase letters until the end
|
[^0-9]*$
|
[^!#$%^&*]*$
|
.*?job corps # any characters and "job corps"
)
[^#]* # characters that are not a #
$ # end of the string
demo
Note: you can write the range #$%& like #-& to win a character.

stribizhev, your answer is correct
^(?!.job corps)(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[!#$%^&])(?!.#).$
can verify the expression in following url:
http://www.freeformatter.com/regex-tester.html

Related

Regex to match a pattern but not two specific cases

I want to match every cases of "-", but not these ones:
[\d]-[A-Z]
[A-Z]-[\d]
I tried this pattern: ((?<![A-Z])-(?![0-9]))|((?<![0-9])-(?![A-Z])) but some results are incorrect like: "RUA VF-32 N"
Can anyone help me?
A simple approach is to use grep with your current logic and inverting the result, and then run another grep to only keep those items that have a hyphen in them:
x <- c("QUADRA 120 - ASA BRANCA","FAZENDA LAGE -RODOVIA RIO VERDE","C-15","99-B","A-A")
grep("-", grep("[A-Z]-\\d|\\d-[A-Z]", x, invert=TRUE, value=TRUE), value=TRUE, fixed=TRUE)
# => [1] "QUADRA 120 - ASA BRANCA" "FAZENDA LAGE -RODOVIA RIO VERDE"
# [3] "A-A"
Here, [A-Z]-\\d|\\d-[A-Z] matches a hyphen either in between an uppercase ASCII etter or a digit or betweena digit and an ASCII uppercase letter. If there is a match, the result is inverted due to invert=TRUE.
See the R demo.
To only match - in all contexts other than in between a letter and a digit, you may use the PCRE regex based on SKIP-FAIL technique like
> grep("(?:\\d-[A-Z]|[A-Z]-\\d)(*SKIP)(*F)|-", x, perl=TRUE)
[1] 1 2
See this regex demo
Details
(?:\d-[A-Z]|[A-Z]-\d) - a non-capturing group that matches either a digit, - and then uppercase ASCII letter, or an uppercase ASCII letter, - and a digit
(*SKIP)(*F) - omit the current match and proceed looking for the next match at the end of the "failed" match
| - or
- - a hyphen.

R regex match things other than known characters

For a text field, I would like to expose those that contain invalid characters. The list of invalid characters is unknown; I only know the list of accepted ones.
For example for French language, the accepted list is
A-z, 1-9, [punc::], space, àéèçè, hyphen, etc.
The list of invalid charactersis unknown, yet I want anything unusual to resurface, for example, I would want
This is an 2-piece à-la-carte dessert to pass when
'Ã this Øs an apple' pumps up as an anomalie
The 'not contain' notion in R does not behave as I would like, for example
grep("[^(abc)]",c("abcdef", "defabc", "apple") )
(those that does not contain 'abc') match all three while
grep("(abc)",c("abcdef", "defabc", "apple") )
behaves correctly and match only the first two. Am I missing something
How can we do that in R ? Also, how can we put hypen together in the list of accepted characters ?
[a-z1-9[:punct:] àâæçéèêëîïôœùûüÿ-]+
The above regex matches any of the following (one or more times). Note that the parameter ignore.case=T used in the code below allows the following to also match uppercase variants of the letters.
a-z Any lowercase ASCII letter
1-9 Any digit in the range from 1 to 9 (excludes 0)
[:punct:] Any punctuation character
The space character
àâæçéèêëîïôœùûüÿ Any valid French character with a diacritic mark
- The hyphen character
See code in use here
x <- c("This is an 2-piece à-la-carte dessert", "Ã this Øs an apple")
gsub("[a-z1-9[:punct:] àâæçéèêëîïôœùûüÿ-]+", "", x, ignore.case=T)
The code above replaces all valid characters with nothing. The result is all invalid characters that exist in the string. The following is the output:
[1] "" "ÃØ"
If by "expose the invalid characters" you mean delete the "accepted" ones, then a regex character class should be helpful. From the ?regex help page we can see that a hyphen is already part of the punctuation character vector;
[:punct:]
Punctuation characters:
! " # $ % & ' ( ) * + , - . / : ; < = > ? # [ \ ] ^ _ ` { | } ~
So the code could be:
x <- 'Ã this Øs an apple'
gsub("[A-z1-9[:punct:] àéèçè]+", "", x)
#[1] "ÃØ"
Note that regex has a predefined, locale-specific "[:alpha:]" named character class that would probably be both safer and more compact than the expression "[A-zàéèçè]" especially since the post from ctwheels suggests that you missed a few. The ?regex page indicates that "[0-9A-Za-z]" might be both locale- and encoding-specific.
If by "expose" you instead meant "identify the postion within the string" then you could use the negation operator "^" within the character class formalism and apply gregexpr:
gregexpr("[^A-z1-9[:punct:] àéèçè]+", x)
[[1]]
[1] 1 8
attr(,"match.length")
[1] 1 1

RegularExpressionValidator for word count that handles punctuation

I thought I had a great regex for limiting the number of words entered into a TextBox, however I discovered, it fails when there is punctuation in the text.
How can I modify this regex (or use a different one) that correctly counts words that may be made up of several sentences or contain other symbols?
^(?:\b\w+\b[\s\r\n]*){1,10}$
This limits the words to 10.
I think this is the ONLY WAY it can be done.
I would like to see a better way if it exists.
(requires an atomic group)
For Unicode:
^\s*(?>[^\pL\pN]*[\pL\pN](?:[\pL\pN_-]|\pP(?=[\pL\pN\pP_-])|[?.!])*\s*){1,10}$
Explained
^
\s*
(?>
[^\pL\pN]* [\pL\pN] # Not letters/numbers, followed by letter/number
(?:
[\pL\pN_-] # Letter/number or '-'
|
\pP # Or, punctuation if followed by punctuation/letter/number or '-'
(?= [\pL\pN\pP_-] )
|
[?.!] # Or, (Add) Special word ending punctuation
)*
\s*
){1,10}
$
For Ascii:
^\s*(?>[\W_]*[^\W_](?:\w|[[:punct:]_-](?=[\w[:punct:]-])|[?.!])*\s*){1,10}$
Expanded
^
\s*
(?>
[\W_]* [^\W_]
(?:
\w
|
[[:punct:]_-]
(?= [\w[:punct:]-] )
|
[?.!]
)*
\s*
){1,10}
$

Regular expression to match version numbers

I need a regular expression that is matching below criteria
For example : below should be matched
1
1134
1.1
1.4.5.6
Those below should not match:
.1
1.
1..6
You can use
^\d+(\.\d+)*$
See demo
^ - beginning of string
\d+ - 1 or more digits
(\.\d+)* - a group matching 0 or more sequences of . + 1 or more digits
$ - end of string.
You can use a non-capturing group, too: ^\d+(?:\.\d+)*$, but it is not so necessary here.

ASP.NET Currency regular expression

The currency required format looks like
1,100,258
100,258
23,258
3,258
Or all integers like
123456 or 2421323 and so on.
I type below in ValidationExpression
(^[0-9]{1,3}(,\d{3})*) | (^[0-9][0-9]*)
But it doesn't work.
Do you have ignore pattern whitespace on? If not, remove the two spaces on each side of the pipe.
Since you're trying to match either, you should stick a marker $ at the end of the string, like so
Also what is the point of ^[0-9][0-9]*, when you can use ^[0-9]+?
^([0-9]{1,3}(?:,\d{3})*|[0-9]+)$
or
^(\d{1,3}(?:,\d{3})*|\d+)$
Explanation:
^ # Anchors to the beginning to the string.
( # Opens CG1
\d{1,3} # Token: \d (digit)
(?: # Opens NCG
, # Literal ,
\d{3} # Token: \d (digit)
# Repeats 3 times.
)* # Closes NCG
# * repeats zero or more times
| # Alternation (CG1)
\d+ # Token: \d (digit)
# + repeats one or more times
) # Closes CG1
$ # Anchors to the end to the string.

Resources