AnujGorasia
AnujGorasia

Reputation: 19

How can I remove substrings that start with 'a' till next 'b'?

I have a string:

a = "select * from ABC -- where  A = B \n And D = C \n -- And  X = Y \n And J = I;"

I want to remove all substrings that start with "--" till the next "\n". So after editing above string, a will be:

a = "select * from ABC  And D = C \n  And J = I;"

Upvotes: 0

Views: 105

Answers (1)

mechnicov
mechnicov

Reputation: 15248

Using String#gsub!

a = "select * from ABC -- where A = B \n And D = C \n -- And X = Y \n And J = I;"
a.gsub(/--.*\n/, "") #=> "select * from ABC  And D = C \n  And J = I;"

It's possible because of newline character.

More general way

For example you need to remove substring starting with "foo" and ending with "bar".

a = "aaafoobb\nbbarcccfoodd\tdbareee"
a.gsub(/foo(.*?)bar/m, "") #=> "aaaccceee"

Upvotes: 5

Related Questions