I am using this ^[S-s][0-9]{4}$ to validate my string, but not working properly. my string has to be in the form of the Letter S (upper-case or lower-case) followed by 4 digits, e.g. S1234. Looks like it works for Letters above S, meaning if I enter w1234 it validates correct, but if I enter a letter below s, like a1234 it doesn’t validate. Thanks.
You need to get rid of the dash:
^[Ss][0-9]{4}$
dashes within [...] denote character ranges. Thus S-s in regex would mean "every character in Unicode character table between S and s" and as those two are not adjacent, you end up with a bunch of matched chars.
Not answer directly the detail content of the question, but whom who end up to this question by the question's title and looking for the answer of regex to find match words begin with specific letter like :
This is a Zone
You should use this regex:
\bd[a-zA-Z]+
[a-zA-Z] should replace by the expected tail you want.
Take a look at this link
[S-s] means the range of all characters between capital S and lowercase s. Try ^[Ss][0-9]{4}$ instead. Or better yet, ^s\d{4}$ with a case-insensitivity modifier (/i in many languages).
Related
This is my RegEx:
"^[^\.]([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)([\.]{0,1})([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)[^\.]#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$"
I need to match only strings less than 255 characters.
I've tried adding the word boundaries at the start of the RegEx but it fails:
"^(?=.{1,254})[^\.]([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)([\.]{0,1})([\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]+)[^\.]#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$"
You need the $ in the lookahead to make sure it's only up to 254. Otherwise, the lookahead will match even when there are more than 254.
(?=.{1,254}$)
Also, keep in mind that you can greatly simplify your regex because many characters that would usually need to be escaped do not need to when in a character class (square brackets).
"[\w-\!\#\$\%\&\'\*\+\-\/\=\`\{\|\}\~\?\^]"
is the same as this:
"[-\w!#$%&'*+/=`{|}~?^]"
Note that the dash must be first in the character class to be a literal dash, and the caret must not be first.
With some other simplifications, here is the complete string:
"^(?=.{1,254}$)[-\w!#$%&'*+/=`{|}~?^]+(\.[-\w!#$%&'*+/=`{|}~?^]+)*#((\d{1,3}\.){3}\d{1,3}|([-\w]+\.)+[a-zA-Z]{2,6})$"
Notes:
I removed the stipulation that the first char shouldn't be a period ([^.]) because the next character class doesn't match a period anyway, so it's redundant.
I removed many extraneous parens
I replaced [0-9] with \d
I replaced {0,1} with the shorthand "?"
After the # sign, it seemed that you were trying to match an IP address or text domain name, so I separated them more so it couldn't be a combination
I'm not sure what the optional square bracket at the end was for, so I removed it: "(]?)"
I tried it in Regex Hero, and it works. See if it works for you.
This depends on what language you are working in. In Python for example you can regex to split a text into separate strings, and then use len() to remove strings longer than the 255 characters you want
I think this post will help. It shows how to limit certain patterns but I am not sure how you would add it to the entire regex.
1- I'm planning to use the regEx to validate user first and last name inputs using this regex:
/^[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]+$/u
However I don't want to allow underscore "_", no only empty space (cannot be left blank) and at least 2 characters. How can I appy them to the regEx above ?
2- For my strong password input validation, I need it be of minimum 8 character length
and it should consist of at least one letter and non-letter ( For e.g. qsgtest123, qsgtest!##)
I will be grateful if you help me with these 2 regExs.
Have a try with:
/^[\p{L},.'-]+[\p{L} ,.'-]*[\p{L},.'-]+$/u
/^((?!_)[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-])+$/u
The above should apply to your first question.
This for the name
/^(?! +$)[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð ,.'-]{2,}$/u
The only difference is the "at least 2 characters" at the end and (?! +$) that means "fail if there are only spaces and end of the string".
Tester: http://gskinner.com/RegExr/?2uv74
And this one for the password:
/^(?=.*[a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð])(?=.*[^a-zA-ZàáâäãåèéêëìíîïòóôöõøùúûüÿýñçčšžÀÁÂÄÃÅÈÉÊËÌÍÎÏÒÓÔÖÕØÙÚÛÜŸÝÑßÇŒÆČŠŽ∂ð]).{8,}$/u
(I'm using your definition of "letter" :-) ). It means:
look forward if present any character any number of times followed by a "letter"
look forward if present any character any number of times followed by a "non-letter"
(these two look forward don't "move" the regex cursor, that is still at the first character)
match any character 8 or more times
I see you are using the /u at the end of the regex. You are probably using Perl. To match any letter you should use \p{L} (and to match any non-letter you should use \P{L}) instead of writing long lists of characters. So the first one would become:
/^(?! +$)[\p{L} ,.'-]{2,}$/u
and the password one:
/^(?=.*\p{L})(?=.*\P{L}).{8,}$/u
And we will ignore the composable diacritics of Unicode :-)
Unless you'd prefer to include them... Then
/^(?! +$)(?=.{2,})(\p{L}\p{M}*|[ ,.'-])*$/u
(we pre-check the absence of all-spaces and the minimum length, and then we check that all the string is composed of letters (each one with an optional zero or more combining mark) or the other symbols in the [])
I want to get all the character's after the second last underscore in a string any ideas how this could be accomplished
Input Output
PART1_PART2_PART3_G2010 PART3_G2010
any idea what the regex should look like
.*_([^_]*_[^_]*)$
Isn't bound to a specific total count of parts between the underscores, like the regex of Andrea Spadaccini is.
edit
The first two symbols .* capture every character, cause . captures one arbitrary character and * is a quantifier for "as much as possible". Then, a underscore should appear.
The expression in brackets should capture the two parts between underscores. Well, at first, we capture all (again the *) non-underscore-characters:
This is done using the square brackets and saying we want any character except (^) the underscore => [^_]. The very last symbol $ defines the end of the input string. I think it is possible to either leave this OR the .* in the beginning out...
Andrea Spadaccini's answer works if you know that the input has three underscores. If the question was meant more generally, referring to everything after the second underscore independent of how many underscores come before that, the regex needs to search from the end ($) like this:
_([^_]*_[^_]*)$
First N not-underscores, than an underscore. Repeat. Group the last characters.
[^_]*_[^_]*_(.*)
can someone please help me to compose a regular expression to check an alphanumeric string is in a particular format.
First character must be a letter and the next 6 characters are numbers...eg x279833 or X279833 are both valid.
This is what i've come up with - ^[A-Za-z]{1}[0-9]{6}$
regards
Yours should work just fine (you edited it in after I wrote this answer), but the {1} is completely unnecessary. You can shorten it a little to use \d instead of [0-9].
If you want to make sure the entire string is that format, use:
^[a-zA-Z]\d{6}$
something like:
^[a-zA-Z]\d{6}$
[a-zA-Z] matches alpha chars
\d matches a numeric char
{6} will match 6 occurrences of the previous token, in this case 6 numeric chars
I don't think I can say anything that hasn't already been considered except to think about international characters. If your first character can also be an alphabetic character from other character sets, you may want to use a predefined character class instead. In that case, you'd have something like this:
^[[:alpha:]]\d{6}$
In my ASP.NET page, I have an input box that has to have the following validation on it:
Must be alphanumeric, with at least one letter (i.e. can't be ALL
numbers).
^\d*[a-zA-Z][a-zA-Z0-9]*$
Basically this means:
Zero or more ASCII digits;
One alphabetic ASCII character;
Zero or more alphanumeric ASCII characters.
Try a few tests and you'll see this'll pass any alphanumeric ASCII string where at least one non-numeric ASCII character is required.
The key to this is the \d* at the front. Without it the regex gets much more awkward to do.
Most answers to this question are correct, but there's an alternative, that (in some cases) offers more flexibility if you want to change the rules later on:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]+)$
This will match any sequence of alphanumerical characters, but only if the first group also matches the whole sequence. It's a little-known trick in regular expressions that allows you to handle some very difficult validation problems.
For example, say you need to add another constraint: the string should be between 6 and 12 characters long. The obvious solutions posted here wouldn't work, but using the look-ahead trick, the regex simply becomes:
^(?=.*[a-zA-Z].*)([a-zA-Z0-9]{6,12})$
^[\p{L}\p{N}]*\p{L}[\p{L}\p{N}]*$
Explanation:
[\p{L}\p{N}]* matches zero or more Unicode letters or numbers
\p{L} matches one letter
[\p{L}\p{N}]* matches zero or more Unicode letters or numbers
^ and $ anchor the string, ensuring the regex matches the entire string. You may be able to omit these, depending on which regex matching function you call.
Result: you can have any alphanumeric string except there's got to be a letter in there somewhere.
\p{L} is similar to [A-Za-z] except it will include all letters from all alphabets, with or without accents and diacritical marks. It is much more inclusive, using a larger set of Unicode characters. If you don't want that flexibility substitute [A-Za-z]. A similar remark applies to \p{N} which could be replaced by [0-9] if you want to keep it simple. See the MSDN page on character classes for more information.
The less fancy non-Unicode version would be
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
^[0-9]*[A-Za-z][0-9A-Za-z]*$
is the regex that will do what you're after. The ^ and $ match the start and end of the word to prevent other characters. You could replace the [0-9A-z] block with \w, but i prefer to more verbose form because it's easier to extend with other characters if you want.
Add a regular expression validator to your asp.net page as per the tutorial on MSDN: http://msdn.microsoft.com/en-us/library/ms998267.aspx.
^\w*[\p{L}]\w*$
This one's not that hard. The regular expression reads: match a line starting with any number of word characters (letters, numbers, punctuation (which you might not want)), that contains one letter character (that's the [\p{L}] part in the middle), followed by any number of word characters again.
If you want to exclude punctuation, you'll need a heftier expression:
^[\p{L}\p{N}]*[\p{L}][\p{L}\p{N}]*$
And if you don't care about Unicode you can use a boring expression:
^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
^[0-9]*[a-zA-Z][a-zA-Z0-9]*$
Can be
any number ended with a character,
or an alphanumeric expression started with a character
or an alphanumeric expression started with a number, followed by a character and ended with an alphanumeric subexpression