My regex is weak, in the case of the following string
"OtherId":47
"OtherId":7
"MyId":47 (Match this one)
"MyId":7
I want to pick up the string that has "MyId" and a number that is not 1 - 9
I thought I could just use:
RegEx: How can I match all numbers greater than 49?
Combined using:
Regular Expressions: Is there an AND operator?
But its not happening... you can see my failed attempt here:
https://www.regextester.com/index.php?fam=99753
Which is
\b"MyId":\b(?=.*^[0-10]\d)
What am I doing wrong?
You can use this regex to match any digit >= 10:
^"MyId":[1-9][0-9]+$
RegEx Demo
If leading zeroes are to be allowed as well then use:
^"MyId":0*[1-9][0-9]+$
[1-9] makes sure number starts with 1-9 and [0-9]+ match 1 or more any digits after first digit.
Essentially, you are looking for 2 or more digits:
\"MyId\"\:(\d{2,})
I have escaped the quotes and colon, and {2,} means 2 or more.
If you need exact match to any number greater than 9
^"MyId":[1-9][0-9]+$
Related
I'm trying to find a regex for numeric inputs. We can receive a leading 0 just if we add a dot for adding 1 or 2 decimal numbers. And of course just accept numbers.
These are the scenarios that we can accept:
0.01
1.1
1.02
120.01
We can't accept these values
0023
0100
.01
.12
Which regex is the best option for these cases?
Until now we try we the following regex for accepting just number and dots
[A-Za-z,]
And also we try with the following ones:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{1})?|(?:,[0-9]{3})*(?:\.[0-9]{1,2})?|(?:\.[0-9]{3})*(?:,[0-9]{1,2})?)$
"/^[-]?[$]\d{1,3}(?:,?\d{3})*\.\d{2}$/"
"/(^(\d{1})\.{0,1}([0-9]){0,2}$)|(^([1-9])\d{0,2}(\,\d{0,3})$)/g"
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
And the next one for deleting the leading zeros but it didn't work for 0.10 cases
^0+
If a negative lookahead is supported, you can exclude matches that start with a zero and have no decimal part.
^(?!0\d*$)\d+(?:\.\d{1,2})?$
^ Start of string
(?!0+\d*$) Negative lookahead, assert not a zero followed by optional digits at the right
\d+ Match 1+ digits
(?:\.\d{1,2})? Match an optional decimal part with 1 or 2 digits
$ End of string
Regex demo
I would go with ^(0|[1-9]\d*|(0|[1-9]\d*)\.\d+)$
You can test here: https://regex101.com/r/oNMgR9/1
Explanation
^ means : match the beginning of the string (or line if the m flag is enabled).
$ means : match the end of the string (or line if the m flag is enabled).
(a|b) means match "a" or match "b" so I'll use this to match either "0" alone or any number not starting with a "0". It's the syntax for a logical or.
. alone is used to match any char. So you have to escape it if you want to match the dot character. This is why I wrote 0\. instead of 0..
[ ] is used to list some characters you want to match. It can be a range if you use the - char, so [1-9] means any digit char from "1" to "9".
\d is to match a digit. It's totally equivalent to [0-9].
* means : match the preceding pattern 0 or many times, so \d* means that it will match 0 or many times a digit, so it will match "8" or "465" or "09" but also an empty string "". If you want to match the preceding pattern at least once or many times then you use + instead of *. So \d+ won't match an empty string "" but \d* would match it.
A) Just a number not starting with 0
[1-9]\d* will match any digit from 1 to 9 and then optionnaly followed by other digits. This will match numbers without a decimal point.
B) Just 0
0 alone is a possibility. This is because the case above isn't covering it.
B) A number with decimals
(0|[1-9]\d*)\.\d+ will match either a "0" alone or a number not starting by "0" and then followed by a point and some other digits (which have to be present because we don't want to match "45." without the numbers behind the dot).
Better alternative
The solution from #TheFourthBird is a bit cleaner with the use of a negative lookahead. It's just a bit different to understand. And he read the question completely: You wanted 1 or 2 digits after the decimal. I forgot about that, so, effectively, \d+ should be replaced by \d{1,2} as you don't want more than 2 digits.
You can use
^(?![0.]+$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - fail the match if there are just zeros or dots till end of string
(?:[1-9]\d*|0) - either a non-zero digit followed with any zero or more digits or a zero
(?:\.\d{1,2})? - optionally followed with a sequence of a . and one or two digits
$ - end of string.
I'm trying to create regex to retrieve last number if there was a number or any number if there wasn't any from a string.
Examples:
6 łyżek stopionego masła -> 6
5 łyżek blabla, 6 łyżek masła -> 6
5 łyżek mąki lub masła -> 5
I'm matching only on masła (changing variable) so it has to be included in regex
EDIT:
I cannot explain what I actually need:
Here is regex101 example: https://regex101.com/r/pEeRk3/1
EDIT2:
Emma's solution works great, but I would need to parse decimals and 2multiple digit numbers as well, meaning that those would match as well:
https://regex101.com/r/pEeRk3/3 - I added examples with answers in the link
If you want to match the last occurence of a digit with a decimal and you word has to follow this value, you might use lookarounds:
(?<!\S)\d+(?:\.\d+)?(?!\S)(?!.*\d)(?=.*masła)
(?<!\S)\d+(?:\.\d+)?(?!\S) Match 1+ digits with an optional past to match a dot and 1+ digits
(?!.*\d) assert that there are no more digits following
(?=.*masła) Assert what is on the right is your word
Regex demo
Or you might use a capturing group:
(?<!\S)(\d+(?:\.\d+)?)[^\d\n]* masła(?!\S)[^\d\n]*$
Regex demo
This expression might simply suffice:
.*([0-9])
if we are interested in one digit only, or
.*([0-9]+)
if multiple digits might be desired.
Demo 1
If those strings with masła are desired, we can expand our expression to:
(?=.*masła).*([0-9])
Demo 2
If we would not be validating our numbers and our number would be valid, with commas or dots, then this expression might likely return our desired output:
(?=.*masła)([0-9,.]+)(\D*)$
Demo 3
I need to be able to check if a string contains either a 2 digit or a 4 digit number before a . (period).
For example, 39. is good, and so is 3926., but 392. is not.
I originally had (^\\d{2,4).$) but that allows between a 2 and a 4 digit number preceding a period.
I also tried (^\\d{2}.|\\d{4}.$) but that didn't work.
You can use this regex:
^\d{2}(?:\d{2})?\.$
This regex makes 2nd set of \d{2} optional thus allowing to match 12. or 1234. but not 123..
In the expression (^\d{2}.|\d{4}.$), the dots match any character.
Try escaping them to make them match literal dots: (^\d{2}\.|\d{4}\.$)
I am trying to write a regular expression that will match a two digit number where the two digits are not same.
I have used the following expression:
^([0-9])(?!\1)$
However, both the strings "11" and "12" are not matching. I thought "12" would match. Can anyone please tell me where I am going wrong?
You need to allow matching 2 digits. Your regex ^([0-9])(?!\1)$ only allows 1 digit string. Note that a lookahead does not consume characters, it only checks for presence or absence of something after the current position.
Use
^(\d)(?!\1)\d$
^^
See demo
Explanation of the pattern:
^ - start of string
(\d) - match and capture into Group #1 a digit
(?!\1) - make sure the next character is not the same digit as in Group 1
\d - one digit
$ - end of string.
I need a regular expression that requires at least ONE digits and SIX maximum.
I've worked out this, but neither of them seems to work.
^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$
^[0-999999]$
Any other suggestion?
You can use range quantifier {min,max} to specify minimum of 1 digit and maximum of 6 digits as:
^[0-9]{1,6}$
Explanation:
^ : Start anchor
[0-9] : Character class to match one of the 10 digits
{1,6} : Range quantifier. Minimum 1 repetition and maximum 6.
$ : End anchor
Why did your regex not work ?
You were almost close on the regex:
^[0-9][0-9]\?[0-9]\?[0-9]\?[0-9]\?[0-9]\?$
Since you had escaped the ? by preceding it with the \, the ? was no more acting as a regex meta-character ( for 0 or 1 repetitions) but was being treated literally.
To fix it just remove the \ and you are there.
See it on rubular.
The quantifier based regex is shorter, more readable and can easily be extended to any number of digits.
Your second regex:
^[0-999999]$
is equivalent to:
^[0-9]$
which matches strings with exactly one digit. They are equivalent because a character class [aaaab] is same as [ab].
^\d{1,6}$
....................
You could try
^[0-9]{1,6}$
it should work.
^[0-9]{1,6}$ should do it. I don't know VB.NET good enough to know if it's the same there.
For examples, have a look at the Wikipedia.
\b\d{1,6}\b
Explanation
\b # word boundary - start
\d # any digits between 0 to 9 (inclusive)
{1,6} # length - min 1 digit or max 6 digits
\b # word boundary - end
^[a-zA-Z0-9]{1,6}$
regex 6 digit number and alphabet in angular
/^[0-9][0-9][0-9][0-9]$/
Enter 4 digit number only