I have the following strings and need to extract the numbers as follows :
1) M123123123AD123 => 123123123
2) M1231231212MN23 => 1231231212
3) G12312312312DD => 12312312312
I am currently reading it using "\d+[0-9]". This works well if there is 1 number after the second set of characters. But if there are multiple numbers after the second character set, the above regex string picks them too. For example, 'M123123123AD123' will give 123123123123. But the last 123 should not be there.
You want to get a streak of digits in between two letters.
You can use
(?<=[a-zA-Z])\d+(?=[a-zA-Z])
See the .NET regex demo.
Or, if you want to get the digits after the leading non-digit chars, use
(?<=^\D+)\d+(?=[a-zA-Z])
See this .NET regex demo.
In C#, you can use Regex.Match:
var result = Regex.Match(text, #"(?<=^\D+)\d+(?=[a-zA-Z])")?.Value;
Regex details:
(?<=[a-zA-Z]) - right before the current location, there must be an ASCII letter (use \p{L} to match any letter)
(?<=^\D+) - right before the current location, there must be start of string + any one or more non-digit chars (use \D* if the digits can appear at the start of string)
\d+ - one or more digits
(?=[a-zA-Z]) - right after the current location, there must be an ASCII letter (use \p{L} to match any letter).
Related
I am trying to write a regex to handle these cases
contains only alphanumeric with minimum of 2 alpha characters(numbers are optional).
only special character allowed is hyphen.
cannot be all same letter ignoring hyphen.
cannot be all hyphens
cannot be all numeric
My regex: (?=[^A-Za-z]*[A-Za-z]){2}^[\w-]{6,40}$
Above regex works for most of the scenarios except 1) & 3).
Can anyone suggest me to fix this. I am stuck in this.
Regards,
Sajesh
Rule 1 eliminates rule 4 and 5: It can neither contain only hyphens, nor only digits.
/^(?=[a-z\d-]{6,40}$)[\d-]*([a-z]).*?(?!\1)[a-z].*$/i
(?=[a-z\d-]{6,40}$) look ahead for specified characters from 6 to 40
([a-z]).*?(?!\1)[a-z] checks for two letters and at least one different
See this demo at regex101
This pattern with i flag considers A and a as the "same" letter (caseless matching) and will require another alpbhabet. For case sensitive matching here another demo at regex101.
You can use
^(?!\d+$)(?!-+$)(?=(?:[\d-]*[A-Za-z]){2})(?![\d-]*([A-Za-z])(?:[\d-]*\1)+[\d-]*$)[A-Za-z\d-]{6,40}$
See the regex demo. If you use it in C# or PHP, consider replacing ^ with \A and $ with \z to make sure you match the entire string even in case there is a trailing newline.
Details:
^ - start of string
(?!\d+$) - fail the match if the string only consists of digits
(?!-+$) - fail the match if the string only consists of hyphens
(?=(?:[\d-]*[A-Za-z]){2}) - there must be at least two ASCII letters after any zero or more digits or hyphens
(?![\d-]*([A-Za-z])(?:[\d-]*\1)+[\d-]*$) - fail the match if the string contains two or more identical letters (the + after (?:[\d-]*\1) means there can be any one letter)
[A-Za-z\d-]{6,40} - six to forty alphanumeric or hyphen chars
$ - end of string. (\z might be preferable.)
I'm trying to come up with a regex to deal with a general scenario to capture a number from a string where the number may have one or more non-numeric characters pre/post-fixed to it.
The number can contain zero or one decimal or comma.
If the string contains multiple "sets" of consecutive digits separated by non-digits I would like the regex to fail ("sets" is probably not the correct terminology).
As an example the following inputs would succeed with a match:
abc12.00xyz would match 12.00
0.1$ would be valid and match 0.1
.01 would be valid and match .01
123abc would be valid and match 123
abc123 would be valid and match 123
These inputs would fail to match:
abc12.00xyz322 would fail due to the second "set" of digits, 322 in this example
12t2 would fail due to having two separate "sets" of digits
I've tried many permutations and I'm not making much headway. This is the closest I've come to so far. It matches on the numbers correctly, excluding the non-digits from the match, but it includes all "sets" of numbers in the string.
([\d]*[.,])?[\d]+
Any suggestions would be appreciated.
You can use a capture group:
^[^0-9\r\n]*?([0-9]*\.?[0-9]+)[^0-9\r\n]*$
^ Start of string
[^0-9\r\n]* Optionally match any char except a digit or a newline, as few as possible
([0-9]*\.?[0-9]+) Capture group 1, match optional digits, optional comma and 1+ digits
[^0-9\r\n]* Optionally match any char except a digit or a newline
$ End of string
See a .NET regex demo (Click on the Table tab to see the capture group values)
I need a regex that checks if a password contains at least one Lowercase letter, at least one Upper case letter, at least two numbers and at least one of(_*&$). This is a MVC project.
This what i have
[RegularExpression(#"(?=\.\*\\d{2})(?=\./*[a-z])(?=\.\*[A-Z])(?=.*[_*&$])", ErrorMessage = "The password must contain at least 1 letter, 2 digits and a special symbol (_*&$)")]
There are a lot of issues with the current regex:
You escaped . and *, and now they denote literal . and * chars, while you wanted to use them as special regex metacharacters
To match at least two digits, you can't just use a \d{2} pattern because it does not match non-consecutive digits, you need \d.*\d or a more efficient (?:\D*\d){2}
You are only using lookaheads, non-consuming patterns, but the RegularExpressionAttribute requires a full string to match the pattern.
Thus, you need
#"^(?=(?:\D*\d){2})(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=[^_*&$]*[_*&$]).*"
Details
^ - start of string
(?=(?:\D*\d){2}) - two not necessarily consecutive digits
(?=[^a-z]*[a-z]) - at least one lowercase ASCII letter
(?=[^A-Z]*[A-Z]) - at least one uppercase ASCII letter
(?=[^_*&$]*[_*&$]) - at least one special char from the _*&$ set
.* - the whole string (with no line breaks) (it gets consumed).
I'm trying to find all Color Hex codes using Regex.
I have this string value for example - #FF0000FF#0038FFFF#51FF00FF#F400FFFF and I use this:
#.+?(?=#)
pattern to match all characters until it reaches #, but it stops at the last character, which should be the last match.
I'm kind of new to this Regex stuff. How could I also get the last match?
Your regex does not match the last value because your regex (with the positive lookahead (?=#)) requires a # to appear after an already consumed value, and there is no # at the end of the string.
You may use
#[^#]+
See the regex demo
The [^#] negated character class matches any char but # (+ means 1 or more occurrences) and does not require a # to appear immediately to the right of the currently matched value.
In C#, you may collect all matches using
var result = Regex.Matches(s, #"#[^#]+")
.Cast<Match>()
.Select(x => x.Value)
.ToList();
A more precise pattern you may use is #[A-Fa-f0-9]{8}, it matches a # and then any 8 hex chars, digits or letters from a to f and A to F.
Don't rely upon any characters after the #, match hex characters and it
will work every time.
(?i)#[a-f0-9]+
im looking for a regex pattern, which matches a number with a length of exactly x (say x is 2-4) and nothing else.
Examples:
"foo.bar 123 456789", "foo.bar 456789 123", " 123", "foo.bar123 " has to match only "123"
So. Only digits, no spaces, letters or other stuff.
How do I have to do it?
EDIT: I want to use the Regex.Matches() function in c# to extract this 2-4 digit number and use it in additional code.
Any pattern followed by a {m,n} allows the pattern to occur m to n times. So in your case \d{m,n} for required values of m and n. If it has to be exactly an integer, use\d{m}
If you want to match 123 in x123y and not in 1234, use \d{3}(?=\D|$)(?<=(\D|^)\d{3})
It has a look ahead to ensure the character following the 3 digits is a non-digitornothing at all and a look behind to ensure that the character before the 3 digits is a non-digit or nothing at all.
You can achieve this with basic RegEx:
\b(\d\d\d)\b or \b(\d{3})\b - for matching a number with exactly 3 digits
If you want variable digits: \b(\d{2,4})\b (explained demo here)
If you want to capture matches next to words: \D(\d{2,4})\D (explained demo here)
\b is a word boundary (does not match anything, it's a zero-match character)
\d matches only digits
\D matches any character that is NOT a digit
() everything in round brackets will capture a match