Regex to delete the last part of the code

Hi everyone,
I would like to use regular expression language to get a new list with only num_num and delete everything after the second _.
I am not practical with this language. How can I do it?
Thank you all

Until somebody with Regex expertise might provide guidance specific to your request …


truncateString.dyn (12.2 KB)

4 Likes

I would do the @Vikram_Subbaiah,

stringval = "14_102_ui(2)_more_under_scores"
delimiter = "_"
output = stringval.split(delimiter, 2)

print output[0] + delimiter + output[1]
#output = 14_102

but if regex is what you want: ^(.*_.*)_.*

3 Likes

Well if we’re putting options on the table:


(edited to add yet another option - down to one line of design script)

3 Likes

Using the expression @3Pinter provided, within a python example.

import re

input = IN[0]
result = []

for i in input:
	result.append(re.split("^(.*_.*)_.*",i)[1])

OUT = result
2 Likes

Thank you all!
Your answers are all solutions.

1 Like