Regex to replace values that include part of match in replacement in sublime?
Asked Answered
O

1

44

I've come up with this regex that finds all words that start with $ and contain _ underscores:

\$(\w+)_(\w+)

I'm basically searching for variables, like $var_foo etc.

How do I replace stuff using the regex groups?

For example, how can I remove the underscore and make the next letter uppercase, like $varFoo ?

Onagraceous answered 31/5, 2012 at 20:49 Comment(0)
T
49

The replacement expression is:

\$\1\u\2

See the Regular Expressions chapter (in the TextMate docs) for more information.

There's already a package that does this, and more:

Tourmaline answered 31/5, 2012 at 20:54 Comment(2)
Sublime text 3 does not take regex in the "Replace With" field. If I put a regex there, it just dumps that regex as string in the editor. How can I use regex in "Replace" field in sublime? Basically, I want to search all words (basically hex colour values) beginning with "#" and add some string before and after that word. Using "\#\w+", I can find all such words, but if I do string1\#\w+string2, it outputs it as it is, without replacing regex with actual word.Olympiaolympiad
@dk49: Use as below: Find: (#\w+) Replace: text-before\1text-afterScientistic

© 2022 - 2024 — McMap. All rights reserved.