Sublime Forum

Regex: Find all lines with a string, but only replace a single char?

#1

EDIT: Found my solution…positive lookbehind assertion.

(?<="fieldName:)*\.

I’m sure this question or a similar one has been asked before, but I’m not even sure how best to describe it, so I can’t say for certain…

I have a json file with N number of keys fieldName. Some of the values of fieldName will have periods in them that I need to replace with underscores.

I managed to do this manually by simply searching for the key as a string, then ctrl-cmd-g to select them all, then expanding all selections to lines. Then I enable Find In Selection, then search for . and replace with _. And it works. But I’d like to save it as a macro, which I can’t do without RegReplace.

A regex of .*"fieldName".* seems to match on all the lines right off the bat, and I don’t even have to use the multi-select key combo or expand the selections to lines. But this is where it breaks down. I can’t seem to then perform my sub-search on ., because as soon as I activate Find In Selection, that breaks the filter and my lines are deselected.

So what’s the best way to do this? Is there a single regex that will match a char that’s in a line with another string, allowing me to replace only the char and not the string? Or is there simply a way to preserve the selection from my regex search, so that I can run a new find/replace with a subsequent search?

Cheers.

0 Likes