Regex Help matching on specific character and position on specific string length - regex

I need to build a regex that does some specific matches but I'm a novice and need some help. I need the regex to match to the 11th character only when the follow criteria are present in the string.
total string is 19 characters long
the first 10 characters are numeric
the 11th character is 1
the 12th - 19th characters are alphanumeric
I need to then replace the 11th character with an "I"

You could use pattern: ^(\d{10})(\d)(\w{8})$
var pattern = new Regex(#"^(\d{10})(\d)(\w{8})$");
var input = "01234567891ABCD1234";
var output = pattern.Replace(input, "$1I$3");
Demo: https://dotnetfiddle.net/oTcIRa

Related

Validating usernames using regular expressions

I have to validate a username in reactJs.
The conditions are-
It should be alphanumeric
Should be greater than 5 characters and less than 11 characters
Should not start with a digit
My solution is not working:
value.match(/^[a-zA-Z][a-zA-Z0-9]{6,10}/)
You just need to change {6,10} to {5,9} since [a-zA-Z] has already represent a character
value.match(/^[a-zA-Z][a-zA-Z0-9]{5,9}$/)
You are already matching 1 character in the first character class [a-zA-Z].
To match greater than 5 characters and less than 11 characters you could use {5,9} as a quantifier for the second character class and assert the end of the line $ to prevent match from returning the first 9 characters when the string is longer than 9 characters.
^[a-zA-Z][a-zA-Z0-9]{5,9}$
Regex demo
const strings = [
"A123456789BBBBBBB",
"A123456789"
];
let pattern = /^[a-zA-Z][a-zA-Z0-9]{5,9}$/;
strings.forEach((value) => {
console.log(value.match(pattern));
});
One method is to use lookaheads to validate the string with some rules.
You could use this pattern ^(?=[a-zA-Z0-9]{5,11}$)(?!\d).+.
This (?=[a-zA-Z0-9]{5,11}$) assures, that what follows beginning of a string ^ is 5 to 11 alphanumerics.
Second, negative, lookahead is (?!\d) to prevent from matching, when digit is first character in a string.
Demo
Read this for reference.

Regex to extract last period and md5 string

I have the following regular expression:
/^[a-f0-9]{8}$/ --- This expression extracts an 8 character string as a md5 hash, for example: if I have the following string "hello world .305eef9f x1xxx 304ccf9f test1232" it will return "304ccf9f"
I also have the following regular expression:
/.[^.]*$/ --- This expression extracts a string after the last period (included), for example, if I have "hello world.this.is.atest.case9.23919sd3xxxs" it will return ".23919sd3xxxs"
Thing is, I've readen a bit about regex but I can't join both expressions in order to find the md5 string after the last period (included), for example:
topLeftLogo.93f02a9d.controller.99f06a7s ----> must return ".99f06a7s"
Thanks in advance for your time and help!
/^[a-f0-9]{8}$/ --- This expression extracts an 8 character string as a md5 hash
Yes but it doesn't return "304ccf9f" from "hello world .305eef9f x1xxx 304ccf9f test1232" because ^ in regex means start of string. How is it possible for it to match in middle of a string?
/.[^.]*$/ --- This expression extracts a string after the last period
No. It will do if you escape first dot only \.
To combine these two you have to replace ^ with \.:
\.[a-f0-9]{8}$
To match your characters 8 times after the last dot in this range [a-f0-9] you might use (if supported) a positive lookahead (?!.*\.) to match your values and assert that what follows does not contain a dot:
\.[a-f0-9]{8}(?!.*\.)
Regex demo
If you want to match characters from a-z instead of a-f like 99f06a7s you could use [a-z0-9]
About the first example
This regex ^[a-f0-9]{8}$ will match one of the ranges in the character class 8 times from the start until the end of the string due to the anchors ^ and $. It would not find a match in hello world .305eef9f x1xxx 304ccf9f test1232 on the same line.
About the second example
.[^.]*$ will match any character zero or more times followed by matching not a dot. That would for example also match a single a and is not bound to first matching a dot because you have to escape the dot to match it literally.
I'm adding this just in case people needs to solve a similar casuistic:
Case 1: for example, we want to get the hexadecimal ([a-f0-9]) 8 char string from our filename string
between the last period and the file extension, in order, for example, to remove that "hashed" part:
Example:
file.name2222.controller.2567d667.js ------> returns .2567d667
We will need to use the following regex:
\.[a-f0-9]{8}(?=\.\w+$)
Case 2: for example, we want the same as above but ignoring the first period:
Example:
file.name2222.controller.2567d667.js ------> returns 2567d667
We will need to use the following regex
[a-f0-9]{8}(?=\.\w+$)

Regex for a string with alpha numeric containing a '.' character

I have not been able to find a proper regex to match any string not starting and ending with some condition.
This matches
AS.E
23.5
3.45
This doesn't match
.263
321.
.ASD
The regex can be alpha-numeric character with optional '.' character and it has to be with in range of 2-4(minimum 2 chars & maximum 4 chars).
I was able to create one ->
^[^\.][A-Z|0-9|\.]{2,4}$
but with this I couldn't achieve mask '.' character at the end of regex.
Thanks.
Maybe not the most optimized but a working one. Created step by step:
The first character should be alphanumeric
^[a-zA-Z0-9]
0, 1 or 2 character alphanumeric or . but not matching end of string
[a-zA-Z0-9\.]{0,2}
an alphanumeric character matching end of string
[a-zA-Z0-9]$
Concatenate all of this to obtain your regex
^[a-zA-Z0-9][a-zA-Z0-9\.]{0,2}[a-zA-Z0-9]$
Edit: This regex allows multiple dots (up to 2)
If I guessed correctly, you want to match all words that are
Between 2 and 4 characters long ...
... and start and end with a character from [A-Z0-9] ...
... and have characters from [A-Z0-9.] in the middle ...
... and are not preceded or followed by a ..
Try this regex to match all these substrings in a text:
(?<=^|[^.])[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9](?=$|[^.])
However, note that this will match the AA in .AAAA.. If you don't want this match, then please give more details on your requirements.
When you are only interested in the number of matches, but not the matched strings, then you could use
(^|[^.])[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9]($|[^.])
If you have one string, and want to know whether that string completely matches or not, then use
^[A-Z0-9][A-Z0-9.]{0,2}[A-Z0-9]$
If there may be at most one . inside the match, replace the part [A-Z0-9.]{0,2} with ([A-Z0-9]?[A-Z0-9.]?|[A-Z0-9.]?[A-Z0-9]?).
You can use this pattern to match what you say,
^[^\.][a-zA-Z0-9\.]{2,4}[^\.]$
Check the result here..
https://regex101.com/r/8BNdDg/3

RegEX string to specific lengths

Hey all I am terrible at RegEX so I am posting this question in hopes that a RegEX Guru will easily know and share the answer.
I have the following string types:
508815 AYBK1619RAUEZP
AWBZ4222TYBE1207CWSWER
DEFAULT EP1 O25R60
And I am needing it in this format (split):
508815 AYBK1619 RAU EZP
AWBZ4222 TYBE1207 CWS WER
DEFAULT EP1 O25 R60
So:
xxxxxxxx xxxxxxxx xxx xxx
First 8 characters in string
Next 8 characters in string
Next 3 characters in string
Last 3 characters in string
I can do the Mid(x,x) and all to do that but I figured that using RegEX would be quicker and cleaner looking code.
Any help would be great! Thanks!
If your desire is to actually use regex to split at those positions, you could use the following:
Dim s As String = "508815 AYBK1619RAUEZP"
Dim m() As String = Regex.Split(s, "(?<=^.{8})|(?<=^.{16})|(?<=^.{19})")
Console.WriteLine(String.Join(" ", m)) '=> "508815 AYBK1619 RAU EZP"
You could also just match the substrings at those positions instead of splitting ...
Dim s As String = "AWBZ4222TYBE1207CWSWER"
Dim m As Match = Regex.Match(s, "^(.{8})(.{8})(.{3})(.{3})$")
If m.Success Then
Console.WriteLine(
String.Join(" ",
m.Groups(1).Value,
m.Groups(2).Value,
m.Groups(3).Value,
m.Groups(4).Value
))
End If
'**Output => "AWBZ4222 TYBE1207 CWS WER"
You can use the following regex to get what you need:
^(\w{0,8})\s*(\w+)\s*(\w{3})(\w{3})$
This regex will:
Match the 0 to 8 word characters from the beginning of the string
Followed by 0 or more spaces
Followed by 1 or more word characters
Followed by 0 or more spaces
Followed by 3 word characters
Followed by 3 word characters
End of string
Word characters (\w) are any alphanumeric character, plus the underscore character. If you strictly want only capital letters for instance, you can replace \w with a character class of A-Z (any letter in the range A-Z), using [A-Z]
See example

Regex Expression using OR and single occurrence

I need a regular expression which satisfies the following conditions.
Should start with a alpha-numeric character
Special characters allowed are - ' and space
Special characters cannot be at the start or end of the string
Special characters cannot occur side by side.
Length of string is 20 characters
Minimum length is 1 character
I am using following regex but missing the fourth point:
^[a-zA-Z0-9] ([-|'] * [a-zA-Z0-9])*${0,20}
You can use this regex:
^[a-zA-Z0-9](?!.*?['-]{2})[a-zA-Z0-9'-]{0,18}[a-zA-Z0-9]$
This assumes that minimum length of string is 2 and max length is 20.
(?!.*?['-]{2}) is negative lookahead that makes sure there no case of 2 consecutive special characters in the string.