Match numbers with - regex

I am having the following list of numbers which I would like to match:
44Th/s
40ksol/s
76Th/s
40Th/s
485Mh/s
432Mh/s
7Th/s
365Mh/s
33Th/s
3.1Th/s
6Th/s
1.1Gh/s
2.4Th/s
1.155Th/s
112.155Gh/s
I am using =regexreplace(A2,"[^\d]","") to match the numbers part. However, when a number looks like that 1.155Th/s I get back 1155.
I also tried the following regex \d*[.]\d*, which only gives me back:
3.1
1.1
24
1.155
112.155
Any suggestions how to also get the numbers part that looks like that 1.155, 112.155 or 3.1?
I appreciate your replies!

I suggest extracting the float/int value using REGEXEXTRACT:
=REGEXEXTRACT(A24, "\d+(?:\.\d+)?")
This will extract the first occurrence of a substring that starts with 1+ digits and is followed with an optional sequence of . and then 1+ digits.

Related

Using Data Validation in Google Sheets to only allow numbers, commas and spaces

I am creating an input google sheet to accept just numbers, commas & spaces - examples listed below. - At a basic level, I just want to exclude the use of A-Z / a-z.
80092382
800
800,876
98672102,20192210
I would like it to exclude anything like:
Hey 01
987 blue
black 1 white
orange
I am getting stuck early on, where I'm trying to only allow text with only numbers in, or excluding anything with letters in.
I have tried the following lines of code within the RegexMatch formula but it either accepts lines with text, or rejects my numbers.
=RegexMatch(L5,"\d")
- This one rejects the numbers.
=RegexMatch(to_text(L5),"\d")
- This rejects only where there are no numbers in the cell - So 'Hey 01' is accepted.
=RegexMatch(to_text(L5),"^\d")
- Same issue, if the cell starts with a number '987 blue' then its accepted
I've attempted a few other ways, such as using the NOT function at the beginning & using other regular expressions. If anyone can point me in the right direction then that would be much appreciated.
Test sheet
You can use
=ARRAYFORMULA(IF(REGEXMATCH(TO_TEXT(C2:C9), "^\d+(,\s*\d+)*$"), "Good", "Error"))
The regex matches
^ - start of string
\d+ - one or more digits
(,\s*\d+)* - zero or more repetitions of
, - a comma
\s* - zero or whitespace
\d+ - one or more digits
$ - end of string.
You can test the content with this type of regex:
=arrayformula(if(regexmatch(to_text(A1:A),"([^\d.,])"),"Error",))

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.

Regex - how to make sure a string contain a word and numbers

I need a little help with Regex.
I want the regex to validate the following sentences:
fdsufgdsugfugh PCL 6
dfdagf PCL 11
fdsfds PCL6
fsfs PCL13
kl;klkPCL6
fdsgfdsPCL13
some chars, than PCL and than 6 or a greater number.
How this can be done?
I'd go with something like this:
^(.*)(PCL *)([6-9][0-9]*|[1-5][0-9]+)$
Meaning:
(.*) = some chars
(PCL *) = then PCL with optional whitespaces afterwards
([6-9][0-9]*|[1-5][0-9]+) then 6 or a greater number
This one should suit your needs:
^.*PCL\s*(?:[6-9]|\d{2,})$
Visualization by Debuggex
In bash:
EXPR=^[a-zA-Z]\+ *PCL *\([6-9]\|[0-9]\{2,\}\)
Translated:
Line begins with at least 1 occurence of a character (ignore caps)
Any amount of spaces, PCL, any amount of spaces
Either a number between 6 or 9, or a number with at least 2 digits
This expression used with something like grep "$EXPR" file.txt will output in stdout the lines that are valid.
This worked well for me. Reads logically too according to the way you described the matching
/[^PCL]+PCL\s?*[6-9]\d*/

How to match a one of a set of numbers?

I am trying to match a group of numbers in regex that consist of one of the following:
1,2,3,4,5,6,7,8,9,10,11
But I am having trouble figuring out the regex.
For single digits this pattern worked fine "0|1|2|3|4|5|6|7|8|9" but it fails on double digit numbers. For example 12 passes as ok due to the regex finding the 1 in 12.
You can use begin and end anchors to force the whole string to be matched:
^(0|1|2|3|4|5|6|7|8|9|10|11)$
Which can be shortened to:
^(\d|10|11)$
This will work if you want to check if just one number is between 0 and 11.
^[0-9]$|^1?[0-1]$
If you want to match a string like:
1,2,3,12,32,5,1,6,8, 11
and match 0-11 then you can use the following:
(?<=,|^)([0-9]|1?[0-1])(?=,|$)
use this regex ^(0|1|2|3|4|5|6|7|8|9|(10)|(11))$

ReGex Phone No Format in Philippines

I'm trying to match exactly the following format:
+639201112222
09201112222
and this is all Ive tried so far:
(\+63|0)?\d{10}
the problem is that it match 2920111222 in 29201112222. How can i create a pattern that will match only the formats below?
+63XXXXXXXXXX
0XXXXXXXXXX
+63 or 0 plus 10 digit number only
where X are all digits from 0-9.
Thank you.
You want to do:
((\+63)|0)\d{10}
A regex for various phone number combinations (just quickly used rubular.com for this):
/(^0|[89]\d{2}-\d{3}\-?\d{4}$)|(^0|[89]\d{2}\d{3}\d{4}$)|(^63[89]\d{2}-\d{3}-\d{4}$)|(^63[89]\d{2}\d{3}\d{4}$)|(^[+]63[89]\d{2}\d{3}\d{4}$)|(^[+]63[89]\d{2}-\d{3}-\d{4}$)/
Validates:
0917-411-1111
0917-4111111
917-411-1111
9174111111
+63917-411-1111
63917-894-3454
639172342345
+639174111111
Here's a solution:
http://jsfiddle.net/kZMRx/