Trying to validate two different format in one regular expression [duplicate] - asp.net

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 5 years ago.
I want to validate these formats in one regular expression in asp.net:
XX-XXXXXXX or XXX-XX-XXXX
These have to be numeric only no characters except the "-".
Is this possible? I've been trying without any success so I want to ask the experts.
Thanks,
Pune

The following should work given your requirements.
"(^\d{2}-\d{7}$)|(^\d{3}-\d{2}-\d{4}$)"

Try something like this:
/^([0-9]{2}-[0-9]{7}|[0-9]{2}-[0-9]{2}-[0-9]{4})$/
[0-9] means any character from 0 to 9.
{X} means X times
| means "or"
- means "-"
and ( and ) delimits a group for replacing
^ and $ delimit the beginning and the ending of the match.

Related

R: Regular Expression for Twitter hashtags? [duplicate]

This question already has answers here:
What characters are allowed in twitter hashtags?
(6 answers)
Closed 4 years ago.
I'm trying to come up with a regular expression that matches Twitter hashtags. Twitter hashtags have the following rules:
1)They cannot contain spaces,
2)They cannot contain punctuation
3) They cannot start with or use only numbers.
This is what I've come up so far, but it still has issues with spaces and punctuation characters:
"#{1}[^0-9]*[^[::punct::]\\s]*?[A-z0-9]*?"
Would appreciate any help with this. Thanks!
Your regex looks a bit complicated, you only need to match the # then a letter and then alphanumeric characters.
You also don't need quantifier for a single character. This should work:
#[a-zA-Z]\w*
If you won't allow underscores (they are legal characters in tweets), use this instead:
#[a-zA-Z][\da-zA-Z]*
It looks like the real spec for a hashtag however is that underscores and numbers are valid anywhere as long as they're at least a letter.
So this would be better:
#\w*[a-zA-Z]\w*
This regex captures only valid hashtags :
(#[a-zA-Z]+[\w]?)(?:\s|$)

Double quotes within character strings [duplicate]

This question already has answers here:
Double quotes not escaped in R
(1 answer)
Get indices of all character elements matches in string in R
(1 answer)
Closed 5 years ago.
I want to do two thing:
1) I want to create a character string with a double quote inside. An example in R would look like follows:
x <- 'vjghvbh"kljnj"kjbn"jk'
[1] "vjghvbh\"kljnj\"kjbn\"jk"
Question 1: How could I create such a character string without the backslash inside?
I tried to use gsub(), but unfortunately that didn't work. I also found some sources, which suggested cat(), but that just prints my character, but does not store it in x.
2) Let's assume that I solved Question 1. Then my character would look like follows:
[1] "vjghvbh"kljnj"kjbn"jk"
Now I need to find the positions of the double quotes. Based on this thread I tried gregexpr(). However, this also did not work, since I was not able to specify the pattern.
Question 2: How could I find the position of the double quotes within my character string?
The result in R should look like this:
[1] 8 14 19

How to edit names using regular expression in R? [duplicate]

This question already has answers here:
Find a word before one of two possible separators
(4 answers)
Closed 8 years ago.
I have names like as following. I just want to keep the part before . . How
>name
uc001aaa.3
uc001aac.4
uc001aae.4
uc001aah.4
uc001aai.1
uc001aak.3
uc001aal.1
uc001aam.4
uc001aaq.2
uc001aar.2
How can I implement this using regex or sub in R ?
I thought this would certainly be a duplicate, but despite the number of gsub question I can't easily find one (e.g. https://stackoverflow.com/questions/23844473/exclude-a-pattern-in-all-collumn-names-in-r). Update: ironically, the closest one is a question the OP asked a few days ago, How to trim the column name of the matrix? ...
Anyway,
gsub("\\.[0-9]$","",name)
does what you want;
\\. specifies a literal . character (one backslash is required to specify that . is literal rather than meaning "any character"; the second is required to protect the first!). As #MatthewLundberg points out you could also use [.] here (. is interpreted literally, rather than as "any character", within the range brackets []).
[0-9] means "a single character in the range 0-9" (not, as you seem to think, the first 9 characters of the string)
$ means "end of string"
So this will remove a dot plus a single number from the end of every string. It doesn't matter how many characters are before the dot. On the other hand, if you might have multiple numeric values, e.g. foo.123, you would need "\\.[0-9]+$ instead (the + means "one or more of the preceding pattern")
Here is a strsplit method, which separates the string on . characters, and keeps the first portion:
sapply(strsplit(name, '[.]'), '[', 1)
## [1] "uc001aaa" "uc001aac" "uc001aae" "uc001aah" "uc001aai" "uc001aak" "uc001aal" "uc001aam" "uc001aaq" "uc001aar"
I'm using the regular expression [.] to match a literal dot rather than \\. because I find it more readable. (It also helps if you have multiple levels of interpretation, but that's not an issue here.)

Password strength via regular express? [duplicate]

This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
I would like to use a regular expression in the ASP.NET membership. What is a regular express for the below?
at least 8 characters long
include at least one upper case letter
one lower case letter
one number
try this..
^((?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,})
You could use something like that:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,}$
Test it here.
You may also want to learn about the "?=" thing, which is called "positive lookahead" here.
In short, when all three lookaheads (.*\d and .*[a-z] and .*[A-Z]) are matched (and are discarded), the main regex [a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,} can be matched too.
Do you have to do this in one regex? I would make each of those rules one regex, and test for them individually. I suspect you code will end up being simpler, and you'll save yourself and whoever has to maintain your application several headaches.

Regular expression - empty or a minimum of 6 characters [duplicate]

This question already has answers here:
What is the expression on RegularExpressionValidator to check if the Textbox text is 6 or more characters long?
(2 answers)
Closed 8 years ago.
I want to use an ASP:RegularExpressionValidator to enforce a minimum character count (6) but only if the field is NOT empty.
Could anyone help me with the regex for this?
Try this regular expression:
^(?:.{6,}|)$

Resources