LewlSauce
LewlSauce

Reputation: 5882

Finding and replacing all strings with variables (ruby)

I noticed that VS Code has the ability to search using regex; however, I'm wondering if there's an easier way to replace all of the symbol assignments in my code with strings. For example:

Alternatively, I've tried to put together a bash script that would do this, but it seems that my matches are going beyond where it should stop. For example:

grep -RE "\[\:.*?\]" .

seems to be a good match, but if the line has more than one ], then it goes to the end. For example, this entire area gets matched:

[:test_recipient] : opts[:recipient:]

as opposed to

[:test_recipient]

and

[:recipient]

individually. How can I only grab up to the end of the first closing bracket?

Upvotes: 0

Views: 32

Answers (1)

The fourth bird
The fourth bird

Reputation: 163457

You can use a negated character class to exclude matching the square brackets

For example

echo "[:test_recipient] : opts[:recipient:]" | grep -Eo "\[:[^][]*]"

Output

[:test_recipient]
[:recipient:]

Upvotes: 1

Related Questions