Regular expression for 6 characters [duplicate] - asp.net

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}$

Related

How to use regex to match upto third forward slash in R using gsub? [duplicate]

This question already has answers here:
How to Select everything up to and including the 3rd slash (RegExp)?
(2 answers)
Extract a regular expression match
(12 answers)
Closed 2 years ago.
So this question is relating to specifically how R handles regex - I would like to find some regex in conjunction with gsub to extract out the text all but before the 3rd forward slash.
Here are some string examples:
/google.com/images/video
/msn.com/bing/chat
/bbc.com/video
I would like to obtain the following strings only:
/google.com/images
/msn.com/bing
/bbc.com/video
So it is not keeping the information after the 3rd forward slash.
I cannot seem to get any regex working along with using gsub to solve this!
The closest I have got is:
gsub(pattern = "/[A-Za-z0-9_.-]/[A-Za-z0-9_.-]*$", replacement = "", x = the_data_above )
I think R has some issues regarding forward slashes and escaping them.
From the start of the string match two instances of slash and following non-slash characters followed by anything and replace with the two instances.
paths <- c("/google.com/images/video", "/msn.com/bing/chat", "/bbc.com/video")
sub("^((/[^/]*){2}).*", "\\1", paths)
## [1] "/google.com/images" "/msn.com/bing" "/bbc.com/video"
You can take advantage of lazy (vs greedy) matching by adding the ? after the quantifier (+ in this case) within your capture group:
gsub("(/.+?/.+?)/.*", "\\1", text)
[1] "/google.com/images" "/msn.com/bing" "/bbc.com/video"
Data:
text <- c("/google.com/images/video",
"/msn.com/bing/chat",
"/bbc.com/video")
Try this out:
^\/[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+
As seen here: https://regex101.com/r/9ZYppe/1
Your problem arises from the fact that [A-Za-z0-9_.-] matches only one such character. You need to use the + operator to specify that there are multiple of them. Also, the $ at the end is pretty unnecessary because using ^ to assert the start of the sentence solves a great many problems.

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

Replace words that start with a period [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
I'm trying to fix a dataset that has some errors of decimal numbers wrongly typed. For example, some entries were typed as ".15" instead of "0.15". Currently this column is chr but later I need to convert it to numeric.
I'm trying to select all of those "words" that start with a period "." and replace the period with "0." but it seems that the "^" used to anchor the start of the string doesn't work nicely with the period.
I tried with:
dataIMN$precip <- str_replace (dataIMN$precip, "^.", "0.")
But it puts a 0 at the beginning of all the entries, including the ones that are correctly typed (those that don't start with a period).
If you need to do as you've stated, brackets [] are regex for 'find exact', or you can use '\\' which escapes a character, such as a period:
Option 1:
gsub("^[.]","0.",".54")
[1] "0.54"
Option 2:
gsub("^\\.","0.",".54")
[1] "0.54"
Otherwise, as.numeric should also take care of it automatically.

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