I'm trying to come up with a Data Annotation regular expression to match the following formats.
34
38-30
100,25-30
4-5,5,1-5
Basically the expression should only allow numbers, -(dash) and ,(comma) in any order
I tried following but couldn't get it working.
[RegularExpression(#"(0-9 .&'-,]+)", ErrorMessage ="Lot numbers are invalid.")]
It's ^[0-9,-]*$. Check out this demo.
I think your use case is having a CSV list of numbers, or ranges of numbers (identified as a number followed by a dash followed by another number). We can use the following regex:
[0-9]+(?:-[0-9]+)?(,[0-9]+(?:-[0-9]+)?)*
This regex matches a number, followed by an optional dash and another number, that quantity then followed by comma and another similar term, any number of times.
In the demo below I added anchors on both sides of the regex. Whether you need to do this depends on how you plan to use the pattern.
Demo
Related
I have a simple question.
I need a regular expression to match a hexdecimal number without colon at the end.
For example:
0x85af6b9d: 0x00256f8a ;some more interesting code
// dont match 0x85af6b9d: at all, but match 0x00256f8a
My expression for hexdecimal number is 0[xX][0-9A-Fa-f]{1,8}
Version with (?!:) is not possible, because it will just match 0x85af6b9 (because of the {1,8} token)
Using a $ also isn't possible - there can be more numbers than one
Thanks!
Here is one way to do so:
0[xX][0-9A-Fa-f]{1,8}(?![0-9A-Fa-f:])
See the online demo.
We use a negative lookahead to match all hexadecimal numbers without : at the end. Because of {1,8}, it is also necessary to ensure that the entire hexadecimal number is correctly matched. We therefore reuse the character set ([0-9A-Fa-f]) to ensure that the number does not continue.
I'm having trouble writing a regex that matches a pattern like this "%n%m%p" or "%n:%m%p". Only allow specific letters and each letter must have percent sign in front of it. No numbers allowed.
This regex /%(n|m|p)$/ works but allows numbers in between. For example this "%n3%p%m" matches. How do I disallow any numbers.
The regex %(n|m|p) itself matches either %n or %m or %p. That the numbers are allowed between each of the parts is most likely because of your other code.
You can match the whole with this regex
/^(%(n|m|p):{0,1}){0,}$/
Just need to be clear about the exact requirements.
The allowed letters are [nmp]
Each letter has to be preceded by a %
There can be an optional : before %
+ One or more tokens from ^ start to $ end
These requirements won't allow any digit.
^(?::?%[nmp])+$
You can test it at regex101
I can't leave a comment but I can answer, so...
It would help to know what exactly you need from this. Do you need those letters in that order? Do you need exactly 3? Or are you looking for any number of any length with any valid characters in between?
That said, one option if you're matching the entire string is
/^(%[nmp][^\d]*)+$/
which should match any %[nmp] with any character between them that isn't a number. Note though that this will match a single %n for example. If you want to match a specific number i or more than a certain number j, change the + to {i} or {j,} respectively.
As long as it has one of the letters and a percent sign it should
match. Just no numbers
Use the following regex pattern:
%[nmp](?!\d)\b
https://regex101.com/r/CrSnFp/2
(?!\d) - negative lookahead assertion, matches one of the specified characters if it's not followed by a number
I have set of strings which looks like the below. Each string has 3 numbers separated with an underscore (_). Each number is a value between 1 - 100.
ma_1_1_1
ma_2_100_59
ma_29_29_29
ma_100_100_100
ma_7_72_78
ma_10_10_100
ma_4_4_49
I want to write a regular expression where I can get the strings whose digits are all same. For example my output would be
ma_1_1_1, ma_29_29_29 and ma_100_100_100
Like this?
^ma_(\d+)_\1_\1$
See a demo on regex101.com.
This uses backreferences with the first captured group as well as anchors.
Use back-references to make a regex match a previous group again:
ma_(100|[1-9][0-9]?)_\1_\1\b
Regex101 Demo
This will also validate that the numbers are within range. If this validation is unnecessary, use (\d+) for the capture group.
This answer is a modification to #4castle which will only extract the strings with similar numbers.
grep("ma_(100|[0-9][0-9]|[0-9])(_\\1)(_\\1)\\b", stringList, value = T)
Is there a way to write regular expression that will match strings like
(0|[1-9][0-9]*)\.[0-9]+
but with a specified number of numeric characters. for example: for 3 numeric characters it should match "0.12", "12.3" but not match "1.234" or "1.2". I know I can write it something like
(?<![0-9])(([0-9]{1}\.[0-9]{2})|([1-9][0-9]{1})\.[0-9]{1})(?![0-9])
but that becomes quite tedious for large number of digits.
(I know I don't need {1} but it better explains what I'm doing)
^(?=[\d.]{4}$)\d+\.\d+$
You can try this for 3 digits.Can be extended for more.See demo.
https://regex101.com/r/bN8dL3/4
or
\b(?=[\d.]{4}\b)\d+\.\d+\b
If you dont want anchors.
You can match them with adding alternatation:
\b(?:[0-9]\.[0-9]{2}|[1-9][0-9]\.[0-9])\b
Then, you won't need any start/end string/line anchors.
See demo
:Statement
Say we have following three records, and we just want to match the first one only -- exactly one digital followed by a specific word, what is the regular expression can be used to make it(in NotePad ++)?
2Cups
11Cups
222Cups
The expressions I tried and their problems are:
Proposal 1:\d{1}Cups
it will find the "1Cups" and "2Cups" substrings in the second and third record respectively, which is what we do not want here
Proposal 2:[^0-9]+[0-9]Cups
same as the above
(PS: the records can be "XX 2Cups", "YY22Cups" and "XYZ 333Cups", i.e., no assumption on the position of the matchable parts)
Any suggestions?
:Reference
[1] The reg definition in NotePad++ (Same as SciTe)
As mentioned in Searching for a complex Regular Expression to use with Notepad++, it is: http://www.scintilla.org/SciTERegEx.html
[2] Matching exact number of digits
Here is an example: regular expression to match exactly 5 digits.
However, we do not want to find the match-able substring in longer records here.
If the string actually has the numbered sequence (1. 2Cups 2. 11Cups), you can use the white space that follows it:
\s\d{1}Cups
If there isn't the numbered list before, but the string will be at the beginning of the line, you can anchor it there:
^\d{1}Cups
Tested in Notepad++ v6.5.1 (Unicode).
It sounds like you want to match the digit only at the start of the string or if it has a space before it, so this would work:
(^|\b)\dCups
Debuggex Demo
Explanation:
(^|\b) Match the start of the string or beginning of a word (technically, word break)
\d Match a digit ({1} is redundant)
Cups Match Cups
This will work:
\b\dCups
If "Cups" must be a whole word (ie not matching 2Cupsizes:
\b\dCups\b
Note that \b matches even if at start or end of input.
I found one possible solution:
Using ^\d{1}Cups to match "Starting with one digital + Cups" cases, as suggested by Ken, Cottrell and Bohemian.
Using [^\d]\dCups to match other cases.
However, haven't found a solution using just one regex to solve the problem yet.
Have a try with:
(?:^|\D)\dCups
This will match xCups only if there aren't digit before.