1

In QGIS attributes I have text strings that sometimes include a measurement as part of descriptions, "such as cut at 10 m". In order to make these look nice when presented in multiline fixed width tables in a layout I would like to replace the spaces with non breaking spaces. I think regexp_replace is the best tool but I have struggled to find a guide to creating the regular expression I can understand. the units are always m so any occurrences of number space m can be replaced with number NBS m this could potentially find a some instances that shouldn't be replaced (such as "near house 21 melted snow is") but they are unlikely to be an issue.

4
  • 1
    Since you are having trouble, you seem to have tried something already - what did you try, and how does the result not suit your needs? Also, please provide the exact settings of your layout, including limiting factors which must be kept in mind when creating the labels.
    – Erik
    Dec 4, 2023 at 11:28
  • 3
    This appears to be a duplicate of your previous Question, but without the useful graphic QGIS: Prevent numbers and their units being split across rows
    – Vince
    Dec 4, 2023 at 12:42
  • Are decimals ever a possibility or are you fortunate enough to work in a world of only whole numbers? Existing answers won't address possibility of decimals.
    – bixb0012
    Dec 4, 2023 at 22:14
  • Fortunately in this instance the numbers are always integers, so the solution is a bit niche.
    – Sethinacan
    Dec 5, 2023 at 8:14

1 Answer 1

5

You can use an expression like:

regexp_replace("fieldname", '(\\d+) m', '\\1 m')

This replaces any number space m (any positive number of digits) with number NBS m, as requested.

The "space" between \\1 and m in the second string needs to be a non-breaking space character. In Windows, you can hold down the Alt key and type 0160 on the numeric keypad to get this.

As per Vincé's suggestion, you can also use char(160) to create the non-breaking space eg:

regexp_replace( "fieldname" , '(\\d+)\\s(m)', '\\1' || char(160) || '\\2')

In the screenshot below, the first line has non-breaking spaces in both columns. The second line has a normal space in the first column, and a non-breaking space in the second. The second column has been created and used in the expression above and run on the first column using the Field Calculator.

enter image description here

5
  • 2
    I think in QGIS expressions, you can denote a non-breaking space using "char(160)" so regexp_replace("fieldname", '(\\d+) \\s(m)', '\\1' || char(160) || '\\2') should do the trick.
    – Vincé
    Dec 4, 2023 at 13:26
  • I have struggled to find a guide to the form of the regular expression required so this helps quite a bit in seeing what is going on
    – Sethinacan
    Dec 4, 2023 at 14:02
  • I found the following link useful in understanding the expressions provided developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…
    – Sethinacan
    Dec 4, 2023 at 21:37
  • 2
    @Sethinacan, QGIS regular expressions are based on Qt's QRegularExpression Class, so I encourage you to use that as a reference since different flavors of regex have different implementations.
    – bixb0012
    Dec 4, 2023 at 23:46
  • @bixb0012 I did find that link when hunting, it is a more definitive source but probably a bit more use once I have got my head around what you can do with regular expressions.
    – Sethinacan
    Dec 5, 2023 at 8:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.