How To Replace a Group With RegEx?

  • 19 April 2018
  • 4 replies
  • 50 views

Userlevel 6
Badge +15

Hey folks!

(And probably, specifically Marian Hatala‌, ‌)

I'm not great with RegEx. I want to use the Replace() functionality to, if a single line of text matches a RegEx pattern, to turn it to a word. 

Using an example here, but basically:

SLT:  32.44

RegEx: ^(dd.)dd$

Now, if I were to put that RegEx into the "Regular Expression" validation portion of the SLT, it's valid - that's rad. Great. If not, maybe just the number appears, or maybe it's blank. Whatever, doesn't matter to me.

BUT.

I want to put it into this calculated field as, for example, replace(SLT, "^(dd.)dd$", "yes this format is correct") 

The goal is to have a specific word appear if the formatting matches the RegEx. 

Thanks for any help that can be offered!

(The overall goal here, if you're curious, is that we have specific validation formatting required if it's imperial vs metric, but it's also all within the same text field. If you have better ideas, I am all ears.)


4 replies

Userlevel 5
Badge +14

To use regex pattern within replace() function (aka in javascript) you have to double each backslash, like:  ^(\d\d\.)\d\d$

not sure what exactly you mean with validation for imperial vs. metric.

But if each can be in different format, you shuold be able to achieve it with regex validation. validation expression would look like: (pattern1|pattern2)

ie. each pattern would validate  one type of formatting

Userlevel 6
Badge +15

Tested, success! Thanks <3

e: Actually -- it seems to be picking up the first pattern in my group but not the latter. Here's my full calc:

if(nIdentifyUnits == "Metric", replace(nMinDepositedWeldMetal,"^(\d\d\.)\d\d$|^(\d\.)\d\d$","Valid"), replace(nMinDepositedWeldMetal,"^(\d.)\d\d\d$","Valid"))

When it's "metric", 55.55 is valid but 5.55 isn't -- I thought that the | would act as "or" (it does in a reg ex right in the control validation itself) and use either of the captures. Any thoughts on that?

Userlevel 6
Badge +15

PS: Marian update your avatar already silly.png

Userlevel 5
Badge +14

yes, "|" means "or" 

regex pattern(s) are correct. haven't you (accidentally) typed/copied a space around figures?

214499_pastedImage_1.png

btw, you could prettify/simplify your regex patterns like

if(nIdentifyUnits == "Metric", replace(nMinDepositedWeldMetal,"^\d{1,2}\.\d{2}$","Valid"), replace(nMinDepositedWeldMetal,"^\d.\d{3}$","Valid"))

and if haven't insisted on what format is for what unit system overall formula could be reduced to

replace(nMinDepositedWeldMetal,"^\d{1,2}\.\d{2}$|^\d\.\d{3}$","Valid")

Reply