String to contain only digits [duplicate] - asp.net

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regex for just only numbers
Javascript regex for allowing only positive digits
How can I check whether a string contains only digits and it's not empty string?
Thank you in advance.

Match against 1 or more digits, anchoring your regex at the beginning and end:
/^\d+$/
^ - anchor at the beginning of the string
\d - match digits (alias for [0-9]
+ - match one or more of the preceding
$ - anchor at the end of the string

Try using a RequiredFieldValidator (which does not allow the field to be empty) instead of a RegularExpressionValidator (which does.)

Related

How do I find the last set of digits in a string [duplicate]

This question already has answers here:
Extract last regex match in R
(2 answers)
Closed 4 months ago.
So let's say I have a string
"Happy 2022 New 01 years!"
I'm looking to return the "01".
To be more specific, I need the last set of digits in the string. This number could just be '1', or '10', or '999'...
The string otherwise could be pretty much anything.
I tried various regex with gsub, but can't seem to get it just right. There is something I misunderstood.
Eg, If I do this:
gsub('.*(\\d+).*$', '\\1', x)
Then why do I get back "1"? Does the '+' in the regex not specify one or more digits?
How is my interpretation wrong?:
'.' for any characters, '(\\d+)' for one or more digits, '.'for some more characters, '$' at the end of the string. gsub is greedy, so it will return the last set of digits (therefore '01', not '2022'). '\\1' will replace the whole string with the first, and only, match. x is the string.
In your regex, a .* will match all the characters(except the newline chars) and thus the whole string is matched. Then, the engine tries to match \d+ but there are no more characters left in the string to match. So, the back-tracking takes place into .* until a digit is found. Once a digit is found(i.e., 1 in your case), \d+ matches the digit and the rest of the string is again matched by .*.
You can try this regex:
\d+(?![^\r\n\d]*\d)
Click for Demo
Explanation:
\d+ - matches 1 or more digits, as many as possible
(?![^\r\n\d]*\d) - negative lookahead to make sure that there are no more digits later in the string
Place word boundaries around the target final number:
x <- "Happy 2022 New 01 years!"
num <- gsub('.*\\b(\\d+)\\b.*$', '\\1', x)
num
[1] "01"
The challenge here is that we're tempted to use a lazy dot to stop at the first digit, e.g. .*?(\\d+).*. But the problem there is that now we will stop at the first number, though we want the last one. So, greedy dot is appropriate, and word boundaries forces the regex to capture the entire final number.
This could work:
(\d+)[^\d]*$
https://regex101.com/r/DHrttA/1
In your solution, I presume the problem is that the first .* is greedy, so it will jump over all it can.
A workaround using strsplit
> tail(strsplit(x, "\\D+")[[1]], 1)
[1] "01"

How to remove '+ off' from the end of string? [duplicate]

This question already has answers here:
How do I deal with special characters like \^$.?*|+()[{ in my regex?
(2 answers)
Closed 4 years ago.
Similar to R - delete last two characters in string if they match criteria except I'm trying to get rid of the special character '+' as well. I also attached a picture of my output.
When I attempt to use the escape command of '+', I get an error message saying
Error: '\+' is an unrecognized escape in character string starting ""\\s\+"
As you noticed, + is a metacharacter in regex so it needs to be escaped. \+ escapes that character, but \, itself, is a special character in R character strings so it, too, needs to be escaped. This is an R requirement, not a regex requirement.
This means that, instead of '\+', you need to write '\\+'.

How to remove beginning-digits only in R [duplicate]

This question already has answers here:
Remove numbers at the beginning and end of a string
(3 answers)
Remove string from a vector in R
(4 answers)
Closed 5 years ago.
I have some strings with digits and alpha characters in them. Some of the digits are important, but the ones at the beginning of the string (and only these) are unimportant. This is due to a peculiarity in how email addresses are stored. So the best example is:
x<-'12345johndoe23#gmail.com'
Should be transformed to johndoe23#gmail.com
unfortunately there are no spaces. I have tried gsub('[[:digit:]]+', '', x) but this removes all numbers, not just the beginning-ones
Edit: I have found some solutions in other languages: Python: Remove numbers at the beginning of a string
As per my comment:
See regex in use here
^[[:digit:]]+
^ Asserts position at the start of the string
You can do this:
x<-'12345johndoe23#gmail.com'
gsub('^[[:digit:]]+', '', x) #added ^ as begin of string
Another regex is :
sub('^\\d+','',x)

Regular expression for 6 characters [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I need couple of regular expression in ASP.NET.
First should accept only 6 characters and second and third character should accept only _ (underscore).
Like: a__cde
And I want one other regex that should also take 6 characters and at second position it should accept only underscore (_) and at third position it should accept maybe underscore (_) or hash (#) and at fourth position it should accept only hash (#).
Note: In both the regex user can only enter: Number, Alphabets or Star (*) at any position instead of above mentioned positions.
Can any one help me out on this? I have tried by below website:
http://www.regexr.com/
But not able to generate proper regex.
Try something like:
1. ^[a-zA-Z0-9\*]__[a-zA-Z0-9\*]{3}$
2. ^[a-zA-Z0-9\*]_[_#]#[a-zA-Z0-9\*]{2}$

RegEx - allow whitespace at end of the string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
I have this patterns:
^\s*(?=\d*(-\d*){0,2}$)(?=(\d-*){13}$).*$
allows strings like "114-4316191466" or "1144316191466" " 1144316191466".
I'd like to allow whitespace also at the end. Best way to do that?
Thanks
a whitespace is \s so to allow one or more whitespaces add \s+ or \s* at the end (as you did for the beginning of the string)

Resources