I have this regex pattern
/^[^-\s][^0-9][a-zA-Z\s-]+$/
I am a bit confused on why when I test it on https://www.regextester.com/
My pattern allows one single number to be added before the string. Meaning that if I type in '2Mantas' it will still accept it whereas '22Mantas' will fail the test. I do not want any numbers or whitespace to be allowed. Any ideas anyone?
You have two negation groups so it is saying the first character cannot be whitespace and the second character cannot be a number. If you put the whitespace and digit in the first brackets it will work as desired.
^[^-\s\d][a-zA-Z\s-]+$
The first two rules in your current regular expression break down to the following:
^[^\s-] - the first character in the string should not be a whitespace or a hyphen. This explains why 2steve is accepted - 2 is not a whitespace or a hyphen character.
[^0-9] - the second character in the strnig should not be a digit. This iexplains why 22steve is not accepted - the 2 in the second position is a digit, which violates this rule.
Assuming you don't want anything but capital and lowercase letters in your first name input, and the name shouldn't start with a whitespace or hyphen character, you can simplify to a subset of your current regular expression:
/^[A-Za-z][A-Za-z-\s]+$/
Regex101
This should work
Get string, that starts with exactly one digit, and after this digits should be expression, that contains only strings (greedy)
^\d{1}([a-zA-Z]+)
https://regex101.com/r/wtBwd7/1
Related
I am new to regex, basically I'd like to check if a word has ONLY one colons or not.
If has two or more colons, it will return nothing.
if has one colon, then return as it is. (colon must be in the middle of string, not end or beginning.
(1)
a:bc:de #return nothing or error.
a:bc #return a:bc
a.b_c-12/:a.b_c-12/ #return a.b_c-12/:a.b_c-12/
(2)
My thinking is, but this is seems too complicated.
^[^:]*(\:[^:]*){1}$
^[-\w.\/]*:[-\w\/.]* #this will not throw error when there are 2 colons.
Any directions would be helpful, thank you!
This will find such "words" within a larger sentence:
(?<= |^)[^ :]+:[^ :]+(?= |$)
See live demo.
If you just want to test the whole input:
^[^ :]+:[^ :]+$
To restrict to only alphanumeric, underscore, dashes, dots, and slashes:
^[\w./-]+:[\w./-]+$
I saw this as a good opportunity to brush up on my regex skills - so might not be optimal but it is shorter than your last solution.
This is the regex pattern: /^[^:]*:[^:]*$/gm and these are the strings I am testing against: 'oneco:on' (match) and 'one:co:on', 'oneco:on:', ':oneco:on' (these should all not match)
To explain what is going on, the ^ matches the beginning of the string, the $ matches the end of the string.
The [^:] bit says that any character that is not a colon will be matched.
In summary, ^[^:] means that the first character of the string can be anything except for a colon, *: means that any number of characters can come after and be followed by a single colon. Lastly, [^:]*$ means that any number (*) of characters can follow the colon as long as they are not a colon.
To elaborate, it is because we specify the pattern to look for at the beginning and end of the string, surrounding the single colon we are looking for that only the first string 'oneco:on' is a match.
I am trying to do do following match using regex.
The input characters should be capital letters starting from 2-10 characters.
If it's 2 characters then allow only those 2 characters which does not contain A,E,I,O,U either at first place or second place.
I tried:
[B-DF-HJ-NP-TV-XZ]{2,10}
It works well, but I am not too sure if this is the right and most efficient way to do regex here.
All credit to Jerry, for his answer:
^(?:(?![AEIOU])[A-Z]{2}|[A-Z]{3,10})$
Explanation:
^ = "start of string", and $ = "end of string". This is useful for preventing false matches (e.g. a 10-character match from an 11 character input, or "MR" matching in "AMRXYZ").
(?![AEIOU]) is a negative look-ahead for the characters A,E,I,O and U - i.e. the regex will not match if the text contains a vowel. This is only applied to the first half of the conditional "OR" (|) regex, so vowels are still allowed in longer matches.
The rest is fairly obvious, based on what you've already demonstrated an understanding about regex in your question above.
I am having problems creating a regex validator that checks to make sure the input has uppercase or lowercase alphabetical characters, spaces, periods, underscores, and dashes only. Couldn't find this example online via searches. For example:
These are ok:
Dr. Marshall
sam smith
.george con-stanza .great
peter.
josh_stinson
smith _.gorne
Anything containing other characters is not okay. That is numbers, or any other symbols.
The regex you're looking for is ^[A-Za-z.\s_-]+$
^ asserts that the regular expression must match at the beginning of the subject
[] is a character class - any character that matches inside this expression is allowed
A-Z allows a range of uppercase characters
a-z allows a range of lowercase characters
. matches a period
rather than a range of characters
\s matches whitespace (spaces and tabs)
_ matches an underscore
- matches a dash (hyphen); we have it as the last character in the character class so it doesn't get interpreted as being part of a character range. We could also escape it (\-) instead and put it anywhere in the character class, but that's less clear
+ asserts that the preceding expression (in our case, the character class) must match one or more times
$ Finally, this asserts that we're now at the end of the subject
When you're testing regular expressions, you'll likely find a tool like regexpal helpful. This allows you to see your regular expression match (or fail to match) your sample data in real time as you write it.
Check out the basics of regular expressions in a tutorial. All it requires is two anchors and a repeated character class:
^[a-zA-Z ._-]*$
If you use the case-insensitive modifier, you can shorten this to
^[a-z ._-]*$
Note that the space is significant (it is just a character like any other).
I need to create a regular expression that matches an ID that has a specific format. The ID always begins with "OR" followed by 4 digits, then a dash, then another number that can be of any length. Examples of valid matches are:
OR1581-2
OR0057-101
OR0000-5312
OR3450-17371
Thanks!
Try ^OR\d{4}-\d+$.
The ^ matches the beginning of the string or line.
OR is not a special sequence and will match only those two characters in order.
\d matches any digit, and {4} is shorthand for listing the preceding group (the digit) exactly four times.
- is not a special character and will match only the hyphen.
\d matches any digit again, and the + requires the preceding group (the digit) to occur one or more times.
$ matches the end of the string or line.
If you need to find match in string that contains such ID, but also other text, then use
\bOR\d{4}-\d+\b
However if you need to verify input if is in such format, so no other text around is allowed, then go with
^OR\d{4}-\d+$
What is the difference between search pattern like [a-zA-Z][a-zA-Z]* and [a-zA-Z]* ?
The first matches one [a-zA-Z] followed by zero or more [a-zA-Z].
The second matches zero or more [a-zA-Z].
The first can also be written as [a-zA-Z]+.
The regex [a-zA-Z][a-zA-Z]* means that you are mandating that there should be one alpabetic character optionally followed by any number of alphabets. On the other hand, [a-zA-Z]* means that the alphabet mandate is entirely off.
For example, your first regex matches the strings azxxx, abccdef but fails 2abcd, 22 and blank strings. But the second regex can match a blank string too.
For the first regex, you may just want to say: [a-zA-Z]+ instead.