3

I have a string field that I want to extract only the digits example: 1,500 m2 -> 1500

example

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)

Taras
35.8k5 gold badges77 silver badges151 bronze badges
asked Mar 18, 2020 at 0:29

5 Answers 5

3

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.

enter image description here

answered Mar 18, 2020 at 1:52
2
  • Thank you!, this worked, but I realized I had blank spaces as well besides commas, is there an expression to solve that too? Commented 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+') Commented Mar 20, 2020 at 3:04
2

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), ',', ''))

example

answered Mar 18, 2020 at 7:43
2

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])','')

enter image description here

answered Jan 27, 2023 at 11:47
1

Try using the regular expression provided in this answer.

regexp_substr( "location", '\b\d[\d,.]*\b' )
answered Mar 18, 2020 at 0:51
1

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

answered Jan 27, 2023 at 13:02
2
  • 1
    Although a valid regular expression, it will not work as-is with regex_replace - QGIS Documentation because "Backslash characters must be double escaped...." Commented Jan 27, 2023 at 13:19
  • @bixb0012 you are absolutely right... I changed my answer Commented Jan 27, 2023 at 15:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.