Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex expression with leading zero

Tags:

regex

I'm rather new to regex expressions and need help with a simple expression. I'm using Pentaho for ETL (Replace in String transformation) and I have column values that I need to add leading zeros to and parse out text as part of the database import. So far I have been unable to add the leading zero.

The column is called Region and the values are "region 8", "region 10", "region 11". My regex expression is ['Region'] which will eliminate the region text but produces results = "8", "10", "11". I need values to produce "08", "10", "11". So all the single digit numbers must have leading zeros.

like image 721
Tone Avatar asked Dec 08 '25 06:12

Tone


1 Answers

Use a look ahead in two steps:

Regex 1:   region (?=\d\D)
Replace 1: 0

Regex 2:   region (?=\d\d)
Replace 2: (nothing)

A look ahead is non-consuming, so you don't have to bother with back references etc.

like image 109
Bohemian Avatar answered Dec 11 '25 01:12

Bohemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!