jrock2004
jrock2004

Reputation: 3501

Groovy with replaceAll with wildcards

Working on a script that will parse some of my java files. I am trying to use replaceAll() to get rid of some of the things that I do not need. So of it is static stuff which is easy, but some are harder.

MyWebsiteTests extends MYbaseTest {

So for this one I want to remove the extends and anything after it on that line. Thanks

Upvotes: 3

Views: 2382

Answers (2)

tim_yates
tim_yates

Reputation: 171194

This would do it:

line.replaceAll( /(.+)\sextends.*$/, '$1' )

Parsing source code with regex always seems to end in tears though...

Upvotes: 5

Ωmega
Ωmega

Reputation: 43703

Groovy:

assert expected == before.replaceFirst(/\s+extends\s.*/, '')

or

assert expected == before.replaceAll(/\s+extends\s.*/, '')

Upvotes: 2

Related Questions