Jackie
Jackie

Reputation: 129

groovy regex to eliminate desired character & all characters succeeding to desired character?

How to eliminate the desired character & all the other characters succeeding that desired character.

def str1 = "value_of_string*123456"

Here, desired character is "*" , and post desired character is "123456". So, after removing them str1 should look like "value_of_string"

Upvotes: 0

Views: 69

Answers (1)

dmahapatro
dmahapatro

Reputation: 50245

def str1 = "value_of_string*123456"

assert str1.takeWhile { it != '*' } == 'value_of_string'
assert str1.tokenize('*')[0] == 'value_of_string'
assert str1.split("\\*")[0] == 'value_of_string'

Upvotes: 2

Related Questions