This question already has answers here:
Regex for validating a string with pre-specified special characters
(3 answers)
Closed 8 years ago.
I have unsuccessfully be looking around the web for such a simple regex and can't seem to put it together.
I need a regex which allows any letter, numbers, whitespace and particular special characters only (# # $ & ( ) - _ /)
"Test #123 #Sample/test" is valid
"Test ^ £300" is not valid
Simply you could try the below regex,
^[\w\s##$&()\/-]+$
DEMO
^ Asserts that we are at the start.
[\w\s##$&()\/-]+ Matches one or more characters from the list.
$ Asserts that we are at the end.
Related
This question already has answers here:
How to match repeated patterns?
(4 answers)
Closed 5 months ago.
I would like to match a string with the following structure string1-string2-string3 where each string can include numerics but no capital letters.
a concrete example would be "hello-world-today"
I have a regex expression, but I am sure it's not optimal since I repeat the same pattern three times.
'([a-z0-9]([-a-z0-9]*[a-z0-9])?)-(([a-z0-9]([-a-z0-9]*[a-z0-9])?))-(([a-z0-9]([-a-z0-9]*[a-z0-9])?))'
This should be an effective way:
^(?:[a-z\d]+-){2}([a-z\d]+)$
It matches two groups of lowercase letters and numbers followed by a dash and then another group of the same characters without the dash at the end.
This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed 2 years ago.
I need to validate Username for an user for below condition.
username can contain letters, numbers or combination of both.
Username must not contain any special characters such as # $ % ^ & * () + = ‘ ~ - _"
Valid Examples:
Test123
123Test
T123est
Invalid Examples:
Test#%^&123
##$Test
Test123##$%^
Below is the expression I tried:
/[a-zA-Z\d]+/
/[^#$%^&()+=‘~-_"]/
This should do it:
/^[A-Za-z\d]+$/
^ and $ marks, start and end of string and are needed to not match part of the input.
Edit: Updated as commenters suggested as I didn't remember that underscore was part of \w
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I have a string in the following format:
ABC12233434343DEF
How can I extract only:
ABC12233434343
I want to leave out the ending set of characters of whatever length they might be.
There are several ways this is one:
.*?\d+
It will match anything at the beginning that is followed by numbers.
It may be also posible to limit the characters it can match initially, like if you was capital letters from A-Z, for example:
[A-Z]+\d+
Online Demo
This question already has answers here:
Regex - Does not contain certain Characters
(2 answers)
Regex: match everything but a specific pattern
(6 answers)
Replace chars if not match
(2 answers)
Closed 4 years ago.
How to do we compose a regex that says "the matches fails if there's a dash somewhere in the middle"
I have several lines that are composed as 3958.3r - 5v and some are without the dash for example: 3958.3v4r. I am able to get the ones with the dash, but not only the ones without the dash
This can be accomplished with a character class negation. ^ at the beginning of a character class simply negates the character class. If you then only have the character -, then you create a character class that matches anything but -.
^[^\-]+$
According to what you have said, you need to put the ^ (start of string) at the front of your pattern, and escape the . or else you will match not only the . after 395[0-9], but any character at all like 3950z.
^395[0-9]\.[^-]+$
This question already has answers here:
Regular Expression for password validation
(6 answers)
Closed 8 years ago.
I can't seem to find a regex to use for a website sign-up form that doesn't require users to enter certain characters.
I need a regex which will allow only the following:
a-z
A-Z
0-9
*#_-
There doesn't have to be a number, no required capitals, no required special characters. Spaces are not allowed.
The minimum length is 8, max is 30 but I'm handling that in another part of the code.
Maybe you could try to match this:
^[a-zA-Z0-9*#_-]{8,30}$
How it works:
everything between ^ and $ means that the entire input has to match the following pattern.
[a-zA-Z0-9*#_-] matches a-z or A-Z or * or # or _ or -
{8,30} will need the previous rule to match at least 8 times, at max 30 times.
This should do it:
^[a-zA-Z0-9*#_-]{8,30}$
This regex will do what you need in most flavours of regex parser:
^[a-zA-Z0-9*#_-]{8,30}$
Try This
^((?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9*#_-]{6,20})$