Suitable Regex Expression with ^ & $ for specific case - regex

There are two scenarios as below:
1234/12345
12345/1234
Client using a regex: ^\d{4}\/\d{4}$
The above regex will not work due to the start and end characters being introduced.
Can someone suggest any modifications to ^\d{4}\/\d{4}$ regex which can extract the above values using start and end pattern i.e. ^ and $ ?

Leverage Regex range ({,}):
^\d{4,5}\/\d{4,5}$
\d{4,5} matches 4 to 5 digits (\d)

Related

Regular Expression to Validate Monaco Number Plates

I would like to have an expression to validate the plates of monaco.
They are written as follows:
A123
123A
1234
I started by doing:
^[a-zA-Z0-9]{1}?[0-9]{2}?[a-zA-Z0-9]{1}$
But the case A12A which is false is possible with that.
You can use
^(?!(?:\d*[a-zA-Z]){2})[a-zA-Z\d]{4}$
See the regex demo. Details:
^ - start of string
(?!(?:\d*[a-zA-Z]){2}) - a negative lookahead that fails the match if there are two occurrences of any zero or more digits followed with two ASCII letters immediately to the right of the current location
[a-zA-Z\d]{4} - four alphanumeric chars
$ - end of string.
You can write the pattern using 3 alternatives specifying all the allowed variations for the example data:
^(?:[a-zA-Z][0-9]{3}|[0-9]{3}[a-zA-Z]|[0-9]{4})$
See a regex demo.
Note that you can omit {1} and
To not match 2 chars A-Z you can write the alternation as:
^(?:[a-zA-Z]\d{3}|\d{3}[a-zA-Z\d]|\d[a-zA-Z\d][a-zA-Z\d]\d)$
See another regex demo.
So it needs 3 connected digits and 1 letter or digit.
Then you can use this pattern :
^(?=.?[0-9]{3})[A-Za-z0-9]{4}$
The lookahead (?=.?[0-9]{3}) asserts the 3 connected digits.
Test on Regex101 here

how to exclude digits using regular expression in VBA

Hello I need to exclude sequence of digits from 890000 till 890001;
890002 to 899999 is acceptable
Is it possible doing using regular expression?
No need for regex.
If Value >= 890002 And Value <= 899999 Then
' Accept
End If
Ok, if you insist on using regex (may be for learning purpose):
In this simple case it is actually easier to exclude those two number and match the rest:
^89(?!000[12])\d{4}$
Explanation:
^ match from start of text
89match 89
(?!000[12]) negative look ahead for 3 times zero and one of characters in the character group (1 or 2). If this doesn't block the match:
\d{4} match 4 digits
$ match end of text.

Regex - Match Collection Must Consume Entire String

Let's say I have the following string:
var goodStr = "abcabcabc";
I want to write a Regex pattern for it to return three (3) matches, which each one's value being "abc". However, if the string deviates from the repeated "abc" pattern at all, I do not want to return ANY matches.
Also, I do not know if there will always be 3 repetitions (there could be any number of repetitions).
For example, the following string should fail, and it should not have any matches:
var badStr = "abcabcabc123";
What pattern should I use that would return 3 matches in goodStr, but 0 matches in badStr?
In other words, the sum of the last match's index and length should equal the total length of the subject string.
I am trying not to use captures/back-references in this scenario also.
EDIT:
The pattern ^(?:abc)+$ does not suffice since it only returns 1 match.
The pattern ^abcabcabc$ does not suffice since it assumes there will only be 3 repetitions of "abc", and I don't know how many repetitions there will be in my scenario. Also, it only returns 1 match.
Solved
With Aaron's and anubhava's help we made this pattern that works for my scenario:
\Gabc(?=(?:abc)*$)
PHP doesn't support dynamic length lookbehind so you may use this regex using \G:
(?:^(?=(?:abc)+$)|(?!^)\G)abc
RegEx Demo
\G asserts position at the end of the previous match or the start of the string for the first match.
After some more iterations this regex turns out to be most efficient:
\Gabc(?=(?:abc)*$)
RegEx Demo 2
You should be able to use the following in C# :
(?<=^(?:abc)*)abc(?=(?:abc)*$)
This matches occurences of abc from which you're able to reach both the start and the end of the string using only other repetitions of abc. This relies on the capacity to use variable-width lookbehinds which is quite rare but that C#'s regex engine implements.
I've been able to test it on http://regexstorm.net/tester where it does return 3 matches for abcabcabc but 0 for abcabcabc123.
You could use \G in combination with a positive lookahead. \G matches at the start of the string or asserts the position at the end of the previous match.
You can capture abc and check if what is on the right is a repetition of the group until the end of the string.
\G(abc)(?=\1*$)
Regex demo

Regex for Chilean RUT/RUN with PCRE

I'm having issues with the validation of the chilean RUT/RUN with a regex expression in PCRE. I have the next regular expression but sadly can't make it work:
\b[0-9|.]{1,10}\-[K|k|0-9]
I need help to see what is wrong with the code. The application I need to use only uses PCRE.
Thank you.
You may use
^(\d{1,3}(?:\.\d{1,3}){2}-[\dkK])$
to match and capture (that is not usually necessary, but your app requires a capturing group to extract its contents) a whole string that matches the pattern. See the regex demo.
To match shorter strings that match this pattern inside a larger string, you may remove ^ and $ (see demo) or use \b word boundaries instead (see this demo).
Details:
^ - start of string
\d{1,3} - 1 to 3 digits
(?:\.\d{1,3}){2} - 2 sequences of a literal . and 1 to 3 digits
- - a hyphen
[\dkK] - a digit, k or K.
$ - end of string.
As they sometimes omit the dots, I used this one:
^(\d{1,2}(?:[\.]?\d{3}){2}-[\dkK])$
Details:
^ - start of string
\d{1,2} - 1 or 2 digits
(?:[.]?\d{3}){2} - 2 sequences of an optional '.' and 3 digits
- a hyphen
[\dkK] - a digit, k or K
$ - end of string
1234567-k OK
12345678-k OK
1.234.567-k OK
12.345.678-k OK
known issue:
12.345678-k and 12345.678-k still OK and I do not like this :(
You need to change to ^(\d{1,3}(?:\.\d{3}){2}-[\dkK])$ to capture only 2 sequence of 3 digits after the first sequence of 1-3 digits.
please consider being more specific in the REGEX build, since it matched wrong numbers, such as 17.87.335-2. Also the included one did't match formats without the dots or the hyphens.
Please consider using the following format: \b(\d{1,3}(?:(.?)\d{3}){2}(-?)[\dkK])\b
Modified prior version to try the other formats: https://regex101.com/r/2Us0j6/9

RegExp: How do I include 'avoid non-numeric characters' from a pattern search?

I want to filter out all .+[0-9]. (correct way?) patterns to avoid duplicate decimal points within a numeral: (e.g., .12345.); but allow non-numerals to include duplicate decimal points: (e.g. .12345*.) where * is any NON-NUMERAL.
How do I include a non-numeral negation value into the regexp pattern? Again,
.12345. <-- error: erroneous numeral.<br/>
.12345(.' or '.12345*.' <-- Good.
I think you are looking for
^\d*(?:\.\d+)?(?:(?<=\d)[^.\d\n]+\.)?$
Here is a demo
Remember to escape the regex properly in Swift:
let rx = "^\d*(?:\\.\\d+)?(?:(?<=\\d)[^.\\d\\n]+\\.)?$"
REGEX EXPLANATION:
^ - Start of string
\d* - Match a digit optionally
(?:\.\d+)? - Match decimal part, 0 or 1 time (due to ?)
(?:(?<=\d)[^.\d\n]+\.)? - Optionally (due to ? at the end) matches 1 or more symbols preceded with a digit (due to (?<=\d) lookbehind) other than a digit ([^\d]), a full stop ([^.]) or a linebreak ([^\n]) (this one is more for demo purposes) and then followed by a full stop (\.).
$ - End of string
I am using non-capturing groups (?:...) for better performance and usability.
UPDATE:
If you prefer an opposite approach, that is, matching the invalid strings, you can use a much simpler regex:
\.[0-9]+\.
In Swift, let rx = "\\.[0-9]+\\.". It matches any substrings starting with a dot, then 1 or more digits from 0 to 9 range, and then again a dot.
See another regex demo
The non-numeral regex delimited character is \D. Conversely, if you're looking for only numerals, \d would work.
Without further context of what you're trying to achieve it's hard to suggest how to build a regex for it, though based on your example, (I think) this should work: .+\d+\D+