regex for a string ,\"16 questions\", - regex

I am not good in regex and I spent so much time figure out how to search for the below pattern:
,\"16 questions\",
This is what I constructed .\"[0-9,]+ questions\".
I think I am close but not sure how much. Can someone please correct it. The numeric value can have comma in it when the number crosses 1k. e.g 2,500 questions.

,"\d{1,3}(,\d{3,3})*\squestions?",
Explanation:
\d{1,3}= 1~3 decimal digits
(,\d{3,3})* = comma and 3 decimal digits, the whole group repeating 0~N times
\s = whitespace
s? = letter s can be missing
These two parts give you accurate recognition of possible numbers.
▶ Test and visualization.
If the backslashes in your text are true backslashes, then the regex including them would be
,\\"\d{1,3}(,\d{3,3})*\squestions?\\",

This works. You didn't indicate if the numeric value could have more than 1 comma (e.g. 1,000,000)
,\\"((\d{1,3})(,\d{3})*)\squestions?\\",

Related

Need assistance with RegEx using Perl and AutoHotKey

I'm trying to read specific values from a TestString using Perl and can't seem to get to where I need to. Hoping someone could help me.
I'm trying to read the value that starts the string and only two numbers to the left of the decimal and save it to value1. It has to be the value that ends two numbers to the left of the decimal and to the start of the string since the leading numbers may be 4, 3, or 2 numbers (e.g. 123420.78616 or 3320.78616).
So with the example below, I'm looking to save "133" to value1 using regexmatch and autohotkey.
The second regexmatch is, I need to save the other portion of the number to value2. Value2 would start two numbers to the left of the decimal and then to the end of the string. So I need the "20.78616" to be saved as value2.
Below I can only capture the full number with the Perl used and I've been trying combinations for hours with a regex101.com to no avail.
Hoping someone could help me.
TestString := "13320.78616"
RegExMatch (TestString, "(([\w\.]+)$)", value1)
RegExMatch (TestString, "(([\w\.]+)$)", value2)
msgbox, %value1%
msgbox, %value2%
Suggest the following regex:
(\d+)(\d\d\.\d*)
Three things to note:
use \d instead of \w if you want to capture just digits and not letters;
the (\d+) captures a leading string of at least one digit, and ends two digits before the decimal because of the next part:
the (\d\d\.\d*) captures exactly two digits, the decimal point, and any following digits.

Regex for validation of a street number

I'm using an online tool to create contests. In order to send prizes, there's a form in there asking for user information (first name, last name, address,... etc).
There's an option to use regular expressions to validate the data entered in this form.
I'm struggling with the regular expression to put for the street number (I'm located in Belgium).
A street number can be the following:
1234
1234a
1234a12
begins with a number (max 4 digits)
can have letters as well (max 2 char)
Can have numbers after the letter(s) (max3)
I came up with the following expression:
^([0-9]{1,4})([A-Za-z]{1,2})?([0-9]{1,3})?$
But the problem is that as letters and second part of numbers are optional, it allows to enter numbers with up to 8 digits, which is not optimal.
1234 (first group)(no letters in the second group) 5678 (third group)
If one of you can tip me on how to achieve the expected result, it would be greatly appreciated !
You might use this regex:
^\d{1,4}([a-zA-Z]{1,2}\d{1,3}|[a-zA-Z]{1,2}|)$
where:
\d{1,4} - 1-4 digits
([a-zA-Z]{1,2}\d{1,3}|[a-zA-Z]{1,2}|) - optional group, which can be
[a-zA-Z]{1,2}\d{1,3} - 1-2 letters + 1-3 digits
or
[a-zA-Z]{1,2} - 1-2 letters
or
empty
\d{0,4}[a-zA-Z]{0,2}\d{0,3}
\d{0,4} The first groupe matches a number with 4 digits max
[a-zA-Z]{0,2} The second groupe matches a char with 2 digit in max
\d{0,3} The first groupe matches a number with 3 digits max
You have to keep the last two groups together, not allowing the last one to be present, if the second isn't, e.g.
^\d{1,4}(?:[a-zA-z]{1,2}\d{0,3})?$
or a little less optimized (but showing the approach a bit better)
^\d{1,4}(?:[a-zA-z]{1,2}(?:\d{1,3})?)?$
As you are using this for a validation I assumed that you don't need the capturing groups and replaced them with non-capturing ones.
You might want to change the first number check to [1-9]\d{0,3} to disallow leading zeros.
Thank you so much for your answers ! I tried Sebastian's solution :
^\d{1,4}(?:[a-zA-z]{1,2}\d{0,3})?$
And it works like a charm ! I still don't really understand what the ":" stand for, but I'll try to figure it out next time i have to fiddle with Regex !
Have a nice day,
Stan
The first digit cannot be 0.
There shouldn't be other symbols before and after the number.
So:
^[1-9]\d{0,3}(?:[a-zA-Z]{1,2}\d{0,3})?$
The ?: combination means that the () construction does not create a matching substring.
Here is the regex with tests for it.

RegEx which accepts only two decimal places

Hi I am working on RegEx. Correct response should NOT allow for number to the tenths only, as in RESPONSE = "925.0", nor should it allow for trailing zeros after the hundredths place as in RESPONSE = "925.000". Only correct responses: 925, 0925, 0925., 925., 925.00, 00925
I worked on it and finally came up with this
"^-?(0)*(\d*(\.(00))?\d+.|(\d){1,3}(,(\d){3})*(\.(00))?)$"
It works for three digit numbers but if i want it for 38400.00 it doesn't allow it
I am not quite certain whether the decimal places can be any digit or if they have to be zero. If the former, then this should do the trick:
^-?\d{1,3}(,?\d{3})*(\.(\d{2})?)?$
If the latter, then this:
^-?\d{1,3}(,?\d{3})*(\.(00)?)?$
The entire match starting with the decimal point is optional, and the two decimal places in that match are optional as well.
UPDATE I just realized that it appears you need to accept commas in the response as well - I assume for thousands, millions, etc.
UPDATE #2 per OP's comment
^-?(\d+|\d{1,3}(,\d{3})*)(\.(00)?)?$
UPDATE #3 Added link to regex101 for explanation of this regular expression.
Have a try with:
^-?\d{1,3}(?:,?\d{3})*(?:\.(?:00)?)?$
I think your problem is that you're trying to match it in chunks of three, with commas separating, but 38400.00 doesn't have commas.
Try this:
^-?\d+(\.?(\d{2})?)$
The - indicates the character, -. With the ? after, it says that it may or may not apply. This allows negative numbers, so if you only want positive numbers matched, delete the first two characters.
\d represents every digit. The + after says that there can be as many as you want, as long as there's at least one.
Then there's a \., which is just a dot in the number. The ? does the same as before.. Since you seem to allow trailing periods, I assumed you wanted it to be considered separately from the following digits.
The () encloses the next group, which is the period (\.) followed by two characters that match \d -- two digits -- and which may be repeated 0 or 1 times, as dictated by the ?. This allows people to either have no digits after the period or two, but nothing else.
The ^ at the beginning specifies it has to be at the beginning of the line, and the $ at the end specifies it has to end at the end of the line. Remember to enable the multiline (m) flag so it works properly.
Disclaimer: I've not done much regex work before, so I could well be totally off. If it doesn't work, let me know.
Couldn't you do this without the ?'s
^[0-9,]+(\.){0,1}(\d{2}){0,1}$
improved: ^\d+[0-9,]*(\.){0,1}(\d{2}){0,1}$
Edit:
Broken down a bit as requested
Old one:
[0-9,]+
1 or more digits/commas (would have accepted ',' as true) so improved version:
\d+
for starts with 1 or more digits
[0-9,]*
0 or more digits/commas
followed by
(\.){0,1}
0 or 1 decimal
Followed by
(\d{2}){0,1}
0 or 1 of (exactly 2 digits)

Vim regex to find numbers that are not part of other words

I'm writing a syntax file for Vim and am looking for a regex to match (so I can highlight) floating point and integer numbers, but not numbers that appear in words, like variable or function names.
Further the language doesn't like floats that end on a decimal point like 5. or 3.e9. Not matching such numbers is acceptable. Matching such numbers separately to mark as Error would also be acceptable.
For instance if I have:
var1 = 3.2e-9
var2 = atan2(5.3, 7e+02)
var3 = -6
Then only 3.2e-9, 5.3, 7e+02, and -6 should be found. It's ok to do it in multiple steps.
For instance this:
[-+]\=\d
gets integers, but it would also match the 1 in var1.
These two expressions:
[-+]\=\d\+\(\.\d\+\)\=\([eE][-+]\=\d\+\)\=
[-+]\=\.\d\+\([eE][-+]\=\d\+\)\=
match both floating point numbers with or without scientific notation and integers. Using these two, we miss miss 5. and 3.e9. (the second is necessary to match numbers that start with a decimal point like .42.
Can I tell the regex about preceding characters?
This comes to mind:
\A[+-]\=\d\+
It would ignore the 1 in var1. But it would also match (5, causing the parentheses to highlight.
My understanding of syntax files has me writing:
syn match myNumber '[-+]\=\d\+\(\.\d\+\)\=\([eE][-+]\=\d\+\)\='
syn match myNumber '[-+]\=\.\d\+\([eE][-+]\=\d\+\)\='
let b:current_syntax = "myLang"
hi def link myNumber Number
Is there a was to do syntax highlighting without regex?
As pointed out in a comment there may be problems with expressions like 2+3. How can I tell that this is not a word?
So what is the best number matching method?
You probably want one of these methods:
Using the beginning-of-word anchor \< (:help /\<)
Using a negative zero-width look-behind: \#<! (:help /\#
Forcing specific sets characters to occur before the digit, but not including them in the match, using \zs (:help /\zs). For example, for only whitespace, newlines, or parentheses you could use \(\_s\|[()]\)\zs[+-]\=\d\+

Regular Expression for a 0.25 interval

My aim is to write a regular expression for a decimal number where a valid number is one of
xx.0, xx.125, xx.25, xx.375, xx.5, xx.625, xx.75, xx.875 (i.e. measured in 1/8ths) The xx can be 0, 1 or 2 digits.
i have come up with the following regex:
^\d*\.?((25)|(50)|(5)|(75)|(0)|(00))?$
while this works for 0.25,0.5,0.75 it wont work for 0.225, 0.675 etc .
i assumed that the '?' would work in a case where there is preceding number as well.
Can someone point out my mistake
Edit : require the number to be a decimal !
Edit2 : i realized my mistake i was confused about the '?'. Thank you.
I would add another \d* after the literal . check \.
^\d*\.?\d*((25)|(50)|(5)|(75)|(0)|(00))?$
I think it would probably just be easier to multiply the decimal part by 8, but you don't consider digits that lead the last two decimals in the regex.
^\d{0,2}\.(00?|(1|6)?25|(3|8)?75|50?)$
Your mistake is: \.? indicates one optional \., not a digit (or anything else, in this case).
About the ? (question mark) operator: Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. (source)
^\d{0,2}\.(0|(1|2|6)?25|(3|6|8)?75|5)$
Regular expressions are for matching patterns, not checking numeric values. Find a likely string with the regex, then check its numeric value in whatever your host language is (PHP, whatever).