Home > Mobile >  Finding and replacing all strings with variables (ruby)
Finding and replacing all strings with variables (ruby)

Time:01-03

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:

  • :variable would convert to 'variable'
  • variable: would convert to 'variable' =>

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?

CodePudding user response:

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:]
  • Related