Can't match string using regular expression - regex

I'm a newbie to regex and I'm trying to come up with a regular expression that matches any string that begins with 2 or 1 number and has to end with a letter: For example: 03C, 4B, 34A,
I came up with this regular expression: ^[0-9]{0,2}\w[A-Z]$ and it works most of the time but it also matches two letters i.e. AA or CD
How can I force at least one number at the beginning of the string? Strings should be no more than 3 characters long and use all uppercase letters.

Try this regular expression
^[0-9]{1,2}[A-Z]$

You are close.
Change your regex pattern to:
^[0-9]{1,2}[A-Z]$
This will match strings that begin with either 1 or 2 numbers, and end with a single uppercase letter.

Related

Regex for String with first two characters fixed and rest digits

Is there a regular expression for? :
String of length 8
First two chracters fixed 'UE' or 'ue'
remaining 6 characters must be digits [0-9]
Eg: https://regex101.com/r/PufypE/1
The expression i tried
\^(UE|ue){2}[0-9]{6}\
but its not working (no match found!)
You want:
\b(UE|ue)[0-9]{6}\b
You don't need the {2} next to the (UE|ue) since you are specifying those exactly. The \b is a word boundary so this will match a list like you put in the comment: UE123456,ue654321 This is a good site to play with a regex on for this kind of stuff: http://regex101.com
Regex should be:
^[Uu][Ee][0-9]{6}$
(UE|ue){2} in your regex would match 2 occurrences of UE or ue

How can i write a regular expression for to match string staring with alphabets and ending with digits

i want to match the strings which is listed below other than than that whatever the string is it should not match
rahul2803
albert1212
ra456
r1
only the above mentioned strings should match in the following group of data
rahul
2546rahul
456
rahul2803
albert1212
ra456
r1
rahulrenjan
r4ghyk
i tried with ([a-z]*[0-9]) but it's not working.
In regular expressions * means zero or more so your regex matches zero letters. If you want one or more use + (\d means digit).
^[a-zA-Z]+\d+$
Regular expressions are fun to solve once you get the hang of the syntax.
This one should be pretty straight:
Start with a letter. ^[a-z] (I am not taking the case of capital
letters here, if they are then ^[a-zA-Z] )
Have multiple letters/digits in between .*
End the string with a digit [0-9]$
Combine all 3 and you get:
^[a-z].*[0-9]$

How this regular expression work?

I want to understand how this regular expression (aka regex) stored in "regex" variable works?
regex='^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$'
I am new to bash scripting and having hard time work with regular expression!
Numbers from 1-9, 0-9, 0-4 and 0-5 are repeated at least twice, which is creating confusion!
Thank you!
Look at this part alone:
[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]
It's a series of alternatives (separated by |), here on separate lines:
[1-9] # Matches 1-9
[1-9][0-9] # Matches 10-99
1[0-9][0-9] # Matches 100-199
2[0-4][0-9] # Matches 200-249
25[0-5] # Matches 250-255
In other words, it matches any number from 1 to 255 inclusive. It's a bit roundabout because regex has no concept of numbers, only of character strings.
The regex attempts to match a four of these numbers with periods between, in order to match a whole IPv4 address.
It looks like someone was trying to match an IPv4 address. The group
([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])
Matches a number from 1 to 255, it then matches a number from 0 to 255 three more times.
(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}
They tried to separate the four numbers with dots. The original regex you posted didn't escape the "." so it would match any character between the four groups. Someone has since edited the regex to fix that character.
The regex is wrapped in ^ and $ to make sure the string contains that and only that. ^ matches the beginning of a string. $ matches the end.

Regular expression for first 4 characters

I need a regular expression for 4 characters. The first 3 characters must be a number and the last 1 must be a letter or a digit.
I formed this one, but it not working
^([0-9]{3}+(([a-zA-Z]*)|([0-9]*)))?$
Some valid matches: 889A, 777B, 8883
I need a regular expression for first 3 will be a number and the last 1 will be a alphabet or digit
This regex should work:
^[0-9]{3}[a-zA-Z0-9]$
This assumes string is only 4 characters in length. If that is not the case remove end of line anchor $ and use:
^[0-9]{3}[a-zA-Z0-9]
Try this
This will match it anywhere.
\d{3}[a-zA-Z0-9]
This will match only beginning of a string
^\d{3}[a-zA-Z0-9]
You can also try this website: http://gskinner.com/RegExr/
It makes it very easy to create and test your regex.
Just take the stars out...
^([0-9]{3}+(([a-zA-Z])|([0-9])))?$
The stars mean zero or more of something before it. You are already using an or (|) so you want to match exactly one of the class, or one of the other, not zero or more of the class, or zero or more of the other.
Of course, it can be simplified further:
^\d{3}[a-zA-Z\d]$
Which literally means... three digits, followed by a character from either lowercase or uppercase a-z or any digit.

Limit number of alpha characters in regular expression

I've been struggling to figure out how to best do this regular expression.
Here are my requirements:
Up to 8 characters
Can only be alphanumeric
Can only contain up to three alpha characters [a-z] (zero alpha characters are valid to)
Any ideas would be appreciated.
This is what I've got so far, but it only looks for contiguous letter characters:
^(\d|([A-Za-z])(?!([A-Za-z]{3,}))){0,8}$
I'd write it like this:
^(?=[a-z0-9]{0,8}$)(?:\d*[a-z]){0,3}\d*$
It has two parts:
(?=[a-z0-9]{0,8}$)
Looksahead and matches up to 8 alphanumeric to the end of the string
(?:\d*[a-z]){0,3}\d*$
Essentially allowing injection of up to 3 [a-z] among \d*
Rubular
On rubular.com
12345678 // matches
123456789
#(#*#$
12345 // matches
abc12345
abcd1234
12a34b5c // matches
12ab34cd
123a456 // matches
Alternatives
I do think regex is the best solution for this, but since the string is short, it would be a lot more readable to do this in two steps as follows:
It must match [a-z0-9]{0,8}
Then, delete all \d
The length must now be <= 3
Do you have to do this in exactly one regular expression? It is possible to do that with standard regular expressions, but the regular expression will be rather long and complicated. You can do better with some of the Perl extensions, but depending on what language you're using, they may or may not be supported. The cleanest solution is probably to check whether the string matches:
^[A-Za-z0-9]{0,8}$
but doesn't match:
([A-Za-z].*){4}
i.e. it's an alpha string of up to 8 characters (first regular expression), but doesn't contain 4 or more alpha characters (possibly separated by other characters (second regular expression).
/^(?!(?:\d*[a-z]){4})[a-z0-9]{0,8}$/i
Explanation:
[a-z0-9]{0,8} matches up to 8 alphanumerics.
Lookahead should be placed before the matching happens.
The (?:\d*[a-z]) matches 1 alphabetic anywhere. The {4} make the count to 4. So this disables the regex from matching when 4 alphabetics can be found (i.e. limit the count to ≤3).
It's better not to exploit regex like this. Suppose you use this solution, are you sure you will know what the code is doing when you revisit it 1 year later? A clearer way is just check rule-by-rule, e.g.
if len(theText) <= 8 and theText.isalnum():
if sum(1 for c in theText if c.isalpha()) <= 3:
# valid
The easiest way to do this would be in multiple steps:
Test the string against /^[a-z0-9]{0,8}$/i -- the string is up to 8 characters and only alphanumeric
Make a copy of the string, delete all non-alphabetic characters
See if the resulting string has a length of 3 or less.
If you want to do it in one regular expression, you can use something like:
/^(?=\d*(?:[a-z]?\d*){0,3}$)[a-z0-9]{0,8}$/i
Which looks for a alphanumeric string between length 0 and 8 (^[a-z0-9]{0,8}$), but first uses a lookahead ((?=\d*(?:[a-z]?\d*){0,3}$)) to make sure that the string
has at most 3 alphabetic characters.