Need assistance with RegEx using Perl and AutoHotKey - regex

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.

Related

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

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?\\",

Regular expression for 7 digit numbers separeted by commas

I need a regular expression to validate a concatenated string that consists of 7 digit numbers separated by commas.
Furthermore, I must ensure that:
The string is not empty.
The chain doesn't begins or finish with commas.
The numbers do not start with 0.
Example: 1234567,2345678,3456789
My solution so far: ^\d+(,\d+)*?$
The problems I still need to resolve:
Validate that the numbers are exactly 7 digits.
Validate that the numbers do not start with 0.
Thank you.
Something like ^[1-9]\d{6}(,[1-9]\d{6})+$ should work. The [1-9] ensures the number doesn't begin with 0, and \d{6} ensures that there are 6 digits to follow.
Based on Gavin answer, here is what worked for me : ^[1-9]\d{6}(,[1-9]\d{6})*$
The minor difference is the use of the * instead of + at the end of the regular expression. There are some cases where I must validate only one 7 digits number...
Thank you for the help everyone!

Regex to match digits with decimals that have spaces as well as no spaces

I am looking for a regex string to match a set of numbers:
9.50 (numbers without spaces, that have 2 to 4 decimal points)
1 9 . 5 0 (numbers with spaces that have 2 to 4 decimals points)
10 (numbers without spaces and without decimal points)
So far I have come up regex string [0-9\s\.]+, but this not doing what I want. Any cleaner solutions out there?
Many Thanks
Try this:
[\d\s]+(?:\.(?:\s*\d){2,4})?
This makes the decimal point and the digits/spaces after it optional. If there are digits after, it checks that there are 2-4 of them with {2,4}
DEMO
If this should only match the whole string, you can anchor it.
^[\d\s]+(?:\.(?:\s*\d){2,4})?\s*$
The problem with your regex is that it will match 127.0.0.1 as well, which is an IP4 address, not a number.
The following regex should do the trick:
[0-9]+[0-9\s]*(\.(\s*[0-9]){2,4})?
Assumption I've made: You need to place at least one digit (before the comma).
regex101 demo.
(\d+[\d\s]*\.((\s*\d){2,4})?|\d+)
I was still getting "trailing spaces" selected with the third example of 10
This eliminated them.
wouldn't this work as well - '[^. 0-9]' ?
my full postgresql query looks like this:
split_part(regexp_replace(columnyoudoregexon , '[^. 0-9]', '', 'g'), ' ', 1)
and its doing the following:
values in the column get everything except numbers, spaces and point(for decimal) replaced with empty string.
split this new char string with split_part() and call which element in the resulting list you want.
was stuck on this for a while. i hope it helps.

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\+

Why my regex is failing for single digits but working for double digits?

I have the requirement to validate a String containing two numbers separated by a dash(-) or a comma(,). Valid values are :
23.98-34.76 or 23.98,34.76
23-34 or 23,34
5-6 or 5,6
I have the following regex which is a slight modification of the answer that I received here in SO. It is covering the 1st and 2nd case above but not the third case involving single digits only.
The modified regex String that I am using is :
(\d+\.?\d+?)([-,])(\d+\.?\d+?)
Where did my regex go wrong?
Correct regex should be like this:
(\d+(\.\d+)?)[-,](\d+(\.\d+)?)
i.e. if there is a period then it is always followed by 1 or more digits.
Otherwise in your regex it will also match strings like 123.,789.