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

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)

Related

R regex using stringr::str_detect and grepl don't seem to be matching "\\+" when it is surrounded by "\\b" [duplicate]

This question already has answers here:
Why does is this end of line (\\b) not recognised as word boundary in stringr/ICU and Perl
(2 answers)
Closed 3 years ago.
I'm pretty new to regex and am trying to detect a word with the "+" symbol when surrounded by "\\b" in long strings of words but both stringr and grepl are giving me the wrong result.
This is the code that I have wrote:
library(stringr)
str_detect("coversyl +", "\\bcoversyl(plus| plus|\\+| \\+)\\b")
The output is FALSE which is wrong.
What would be the right way to do it?
My guess is that your expression is just fine, maybe missing an space,
\\bcoversyl\\b\\s(\\bplus\\b|\\+)
Please see the demo for additional explanation.
If we might want more than one space, we would simply change \\s to \\s+ and it might work:
\\bcoversyl\\b\\s+(\\bplus\\b|\\+)

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 this gsub() works? [duplicate]

This question already has answers here:
Reference - What does this regex mean?
(1 answer)
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
So, it's the code and I don't understand the output.
Original theStr:"C:\\Users\\codep\\Desktop\\DM_HW2\\2017-07-9.csv"
gsub("^(.*)\\\\.*$",'\\1',theStr)
it become:"C:\\Users\\codep\\Desktop\\DM_HW2"
what is the "\\\\." in the pattern and the '\\1' in the replacement?
Your pattern may be explained as follows:
^(.*)\\\\ - match and capture everything up but excluding the LAST path separator
.*$ - then match/consume the remainder of the file path
Then, you replace the original input with the captured quantity, which is \\1, the second parameter passed to gsub. This effectively removes everything from the final path separator to the end of the file path.
Here is a regex demo which you can use to see for yourself how the pattern is matching, and what the capture group is:
Demo

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)

String to contain only digits [duplicate]

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.)

Resources