Regular expression to validate a string representing either number or addition of numbers - asp.net

Help on validation expression to validate a string representing either number or addition of numbers.
e.g:
2 OK
22 + 3 OK
2+3 not OK
2 +3 not OK
2 + 34 + 45 OK
2 + 33 + not OK
2 + 33+ 4 not OK

This would be quite a simple pattern
^\d+(?: \+ \d+)*$
See it here on Regexr
^ anchor for the start of the string
$ anchor for the end of the string
The anchors are needed, otherwise the pattern will match "partly"
\d+ is at least one digit
(?: \+ \d+)* is a non capturing group that can be there 0 or more times (because of the * quantifier at the end)

Try:
/^\d+(\s+\+\s+\d+)*$/
This matches a number followed by an optional plus sign and number, which can then be repeated.

Related

split a string on every odd positioned spaces

I have strings of varying lengths in this format:
"/S498QSB 0 'Score=0' 1 'Score=1' 2 'Score=2' 3 'Score=3' 7 'Not administered'"
the first item is a column name and the other items tell us how this column is encoded
I want the following output:
/S498QSB
0 'Score=0'
1 'Score=1'
2 'Score=2'
3 'Score=3'
7 'Not administered'"
str_split should do it, but it's not working for me:
str_split("/S498QSB 0 'Score=0' 1 'Score=1' 2 'Score=2' 3 'Score=3' 7 'Not administered'",
"([ ].*?[ ].*?)[ ]")
You can use
str_split(x, "\\s+(?=\\d+\\s+')")
See the regex demo.
Details:
\s+ - one or more whitespaces
(?=\d+\s+') - a positive lookahead that requires the following sequence of patterns immediately to the right of the current location:
\d+ - one or more digits
\s+ - one or more whitespaces
' - a single quotation mark.

Regex to check character and integer inside a string

I have a data frame and I want to flag all the elements inside this which contains character or character plus integer value.
After a little bit of search, I could make this regex but its not giving the expected output:
([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*
Expected output
alpha - True
Alpha1 - True
A35. 1ha-True
Alp1Ha - True
A pha6- True
12345 - False
0 - False
-23442 - False
Try this : demo
^(?=.*[a-zA-Z]).+$
You may use
^[^A-Za-z]*[A-Za-z].*$
See the regex demo
Details
^ - start of a string
[^A-Za-z]* - 0 or more chars other than ASCII letters
[A-Za-z] - an ASCII letter
.* - any 0+ chars other than line break chars as many as possible
$ - end of string.

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 "#"

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

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