Sachith Muhandiram
Sachith Muhandiram

Reputation: 2972

Multiple replace regex in one Apache-NiFi statement

I have a csv in following format.

id,mobile
1,02146477474
2,08585377474
3,07646474637
4,02158789566
5,04578599525

I want to add a new column and add just leading 3 numbers to that column (for specific cases and all the others NOT_VALID string). So result should be:

id,number,provider
1,02146477474,021
2,08585377474,085
3,07646474637,NOT_VALID
4,02158789566,021
5,04578599525,NOT_VALID

I can use following regex for replacing that. But I would like to use all possible conversations in one step. Using UpdateRecord processor.

${field.value:replaceFirst('085[0-9]+','085')}

When I use something like this:

${field.value:replaceFirst('085[0-9]+','085'):or(${field.value:replaceFirst('086[0-9]+','086')}`)}

This replaces all with false.

Upvotes: 0

Views: 3537

Answers (1)

daggett
daggett

Reputation: 28564

Nifi uses Java regex

As soon, as you are using record processing, this should work for you:

${field.value:replaceFirst('^(021|085)?.*','$1')}

The group () optionally ? catches 021 or 085 at the beginning of string ^

The replacement - $1 - is the first group


PS: The sites like https://regex101.com/ helps to understand regex

Upvotes: 2

Related Questions