I am trying to extract number only (float?) from accounting numbers in google sheet with abbrev. units like K,M,B and sometimes in a bracket when negative. Sorry I am so new in regex, how to write a regular express covering different possibilities like (213M),(31.23B)?
\(([0-9.]+\.\[0-9.]+)\)
You may use
\((-?\d+(?:\.\d+)?)[KMB]\)
Details
\( - a literal ( char
(-?\d+(?:\.\d+)?) - Group 1:
-? - an optional -
\d+ - 1+ digits
(?:\.\d+)? - an optional non-capturing group matching one or zero occurrences of a dot followed with 1+ digits
[KMB] - a character class matching K, M or B
\) - a literal ) char.
See the regex demo.
Related
I need to take only a number (a float number) from a text, but I can't remove the whitespaces...
** Update
I have a problem with this method, I only need to consider numbers and ',' between '- EUR' and 'Fee' as rule.
You can use
- EUR\W*(.*?)\W*Fee
See the regex demo.
Variations of the regex that might work in different regex engines:
- EUR\W*\K.*?(?=\W*Fee)
(?<=- EUR\W*).*?(?=\W*Fee)
Details:
- EUR - literal text
\W* - zero or more non-word chars
(.*?) - Group 1: any zero or more chars other than line break chars as few as possible
\W*- zero or more non-word chars
Fee - a string.
You could also match the number format in capture group 1
- EUR\b\D*(\d+(?:,\d+)?)\s+Fee\b
- EUR\b Match - EUR and a word boundary
\D* Match 0+ times any char except a digit
( Capture group 1
\d+(?:,\d+)? Match 1+ digits with an optional decimal part
) Close group 1
\s+Fee\b Match 1+ whitespace chars, Fee and a word boundary
Regex demo
this is working i removed the , from (.) in test string.
Regex example - working
i want to select all money numbers (red) and this is regex which doesn't work
I can improve regex so that it'll select all numbers ranges like this
May I use some post select regex to remove all dates/time from it?
like where it
doesn't contain '/'
and double '.'
and ':'
You may use
\b(?<!\d[:\/.])\d+(?:,\d+)*(?:\.\d+)?(?![.\/:]?\d)
See the regex demo
Regex details
\b - a word boundary
(?<!\d[:\/.]) - no digit + :, / or . allowed immediately to the left of the current location
\d+ - 1+ digits
(?:,\d+)* - zero or more occurrences of , and 1+ digits
(?:\.\d+)? - an optional occurrence of . and 1+ digits
(?![.\/:]?\d) - no optional ., / or : followed with a digit are allowed immediately to the right of the current location.
Could somebody help me with regex? Basically, I would like a regex which matches with decimal numbers.
Allowed types:
12
1.3234
0.3423434
23423.12
Not allowed types:
0012
12.324.12
01.2332
.12
121212.
Thanks for your help in advance!
Regards!
This regex should do your job.
^(?:[1-9][0-9]*|0)(?:\.[0-9]+)?$
Demo
^ - Start of string
(?: - Beginning of a non-capture group
[1-9][0-9]* - Matches a number not starting with a zero
|0 - Or allows matching only a zero to allow numbers like 0.3423434 or 0.1
(?:\.[0-9]+)? - Optionally allows a decimal part in the number
$ - End of string
Edit: Based on Allan's suggestion to use \d instead of [0-9]
In case your input only contains English numbers, or when you only intend to match English numbers, then using [0-9] should be preferred for reasons like better performance and wide supported of it across regex engines.
But in case your input contains digits from other languages like Hindi १ २ etc. and you want to match any digits, then \d should be preferred instead of [0-9]
That would be
^[[:digit:]]+(\.[[:digit:]]+)?$
Beginning of the string, then one or more digits, then (optionally) a dot and one or more digits, then the end of the string.
If you want to cater for the sign, add [+-]? after the ^.
You could use an alternation
^(?:0\.\d*[1-9]\d*|[1-9]\d*(?:\.\d+)?)$
Regex demo
Explanation
^ Start of string
(?: Non capturing group
0\. Match a single zero followed by a dot
\d*[1-9]\d* Match 0+ digits, a digit 1-9 and again 0+ digits t prevent only zeroes to match
| Or
[1-9]\d* Match a digit 1-9 followed by 0+ digits
(?:\.\d+)?) Optional part to match a dot and 1+ digits
) Close non capturing group
I have a text with a number that contains dots:
text 304.33.44.52.03.001 text
where I want to capture the number including strings:
304.33.44.52.03.001
The following regex will capture sevaral groups:
(\d+\.?)
Resulting in:
304.
33.
44.
...
What is the correct syntax to return the entire number including dots in one result?
\d+\.? matches 1+ digits and then an optional . char.
You need to use either
\d+(?:\.\d+)*
or
\d[\d.]*
See the regex demo
The \d+(?:\.\d+)* pattern matches
\d+ - 1+ digits
(?:\.\d+)* - 0 or more occurrences of a . and then 1+ digits. (?:...) is a non-capturing group that is used to group 2 patterns and set a quantifier on their sequence.
The \d[\d.]* pattern matches a digit first, and then tries to match 0 or more digits or ..
In regex engines that do not support \d you need to use a safer pattern, a bracket expression [0-9].
Hello i want to match with regex this word
(Parc Installé)
from this text:
31/1/2017 17:19:23,4245986,ct0001#Intotel.int,Parc Installé,100.100.30.100
I did this regex ',[A-Za-zA-zÀ-ú+ \/\w+0-9._%+-]+,'
But the result is : 4245986 ans Parc Installé.
How can i match only Parc Installé
You may try a regex based on a lookahead that will require a comma and digits/commas after it up to the end of string:
[^,]+(?=\s*,[\d.]+$)
See this regex demo
Details:
[^,]+ - 1 or more chars other than ,
(?=\s*,[\d.]+$) - a lookahead requiring
\s* - zero or more whitespaces
, - a comma
[\d.]+ - 1+ digits or dots up to...
$ - ... the end of string
To make it a bit more restrictive, you may replace the lookahead with (?=\s*,\d+(?:\.\d+){3}$) to require 4 sequences of dot-separated 1+ digits. See this regex demo.
If a lookahead is not supported (case with a RE2 engine), you might want to use a capturing group based solution:
([^,]+)\s*,[\d.]+$
Here, the part within (...) will be captured into Group 1 and will be accessible via a backreference or a function like =REGEXEXTRACT in Google Spreasheets that only retrieves the contents of a capturing group if the latter is present in the pattern.