Skip to main content
Question

How to split an attribute using regular expression

  • July 14, 2021
  • 3 replies
  • 394 views

ingalla
Contributor
Forum|alt.badge.img+10

I am trying to split an attribute using a regular expression as there may be differences within the attribute.

 

For Example the attribute may be as AA9A or B7B

However some of the attributes are formatted as CC7 or DD6 I therefore do not want to remove the last character from these, all I need to extract is AA9 or B7.

 

Is there a way with a regular expression to search for a character, potential character and then a digit?

Or is there a better way to achieve what i am trying to do??

Thanks in advance

Andy

 

3 replies

jkr_wrk
Influencer
Forum|alt.badge.img+35
  • 424 replies
  • July 14, 2021

There could be a simple solution but I go with StringSearcher with regex ^[A-Z]*[0-9]* or something like that.

Then you could replace the found string with the original if you want an attribute with just the A and the B in an AttributeCreator.


geomancer
Evangelist
Forum|alt.badge.img+58
  • Evangelist
  • 932 replies
  • July 14, 2021

In the StringReplacer you can use the regular expression 

\D$

(all characters at the end of the attribute that are not numbers), and replace this with nothing.

 


hkingsbury
Celebrity
Forum|alt.badge.img+63
  • Celebrity
  • 1625 replies
  • July 14, 2021

I'd use the string searcher with the following REGEX

([a-zA-Z]{1,2}\d)

[a-zA-Z]{1,2} matches one or two characters (A-Z case insensitive)

\d matches one digit

(...) groups everything together

 

https://rubular.com/r/h9ItOkxPbyldAZ