I have a string field that I want to extract only the digits example: 1,500 m2 -> 1500
I've tried with regexp_substr("location", '(\\d*)')
to remove all after the blank space, but I need also the ones that have a comma to only extract the digits, the first two rows work fine for me, but the two after doesn't (1,500 M2
and 1,456 m2
)
5 Answers 5
Another regex expression to use is the following:
regexp_substr("FieldName",'\\d,+\\d+|\\d+')
Substitute "FieldName"
with the name of the field that you want to extract the digits from. "descriptio"
in your case.
The above will extract digits with comma or digits without comma.
-
Thank you!, this worked, but I realized I had blank spaces as well besides commas, is there an expression to solve that too?Juan Millan– Juan Millan2020年03月19日 15:40:51 +00:00Commented Mar 19, 2020 at 15:40
-
@JuanMillan I have not try this but you can give it a try:
regexp_substr("FieldName",'\\d , \\d+|\\d,+\\d+|\\d+')
ahmadhanb– ahmadhanb2020年03月20日 03:04:27 +00:00Commented Mar 20, 2020 at 3:04
Probably a bit old-fashion approach but also gives a desired output using the follwoing expression
to_int(replace(left("text", regexp_match("text",' ') - 1), ',', ''))
An elegant way to remove 1) the string 'M2'
plus 2) all characters except numbers is this epression. Like this, you remove commas, letters, all whitespaces etc., but keep all number (except the 2
in M2
):
regexp_replace (Elev,'(M2|[^0-9])','')
To match uppercase M2
as well as lowercase m2
, instead of (M2|
use (M|m)2|
:
regexp_replace (Elev,'((M|m)2|[^0-9])','')
Try using the regular expression provided in this answer.
regexp_substr( "location", '\b\d[\d,.]*\b' )
Another more general solution:
regexp_replace("location",'\\b(\\d+)[.,]?(\\d*)\\b.+','\1円\2円')
... and many other regex here: https://stackoverflow.com/questions/12117024/decimal-number-regular-expression-where-digit-after-decimal-is-optional
-
1Although a valid regular expression, it will not work as-is with regex_replace - QGIS Documentation because "Backslash characters must be double escaped...."bixb0012– bixb00122023年01月27日 13:19:03 +00:00Commented Jan 27, 2023 at 13:19
-
@bixb0012 you are absolutely right... I changed my answerchristoph– christoph2023年01月27日 15:35:56 +00:00Commented Jan 27, 2023 at 15:35
Explore related questions
See similar questions with these tags.