iadcialim24
iadcialim24

Reputation: 4015

Replace string outside "" only in Sublime using regular expression

I have a long list (about 100k lines) of strings in my file like this:

job_title <- map["job_title"]
....

I want to do some replacement to make it like this:

jobTitle <- map["job_title"]

so I just replace _t to T, but I want the left side only.

Upvotes: 1

Views: 282

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626927

If you replace _t with T, you will replace all occurrences of _t with T regardless of the context.

What you may try is to match all "..." substrings and skip them, and then match any letter after _ and convert it to uppercase. How you match "..." substrings depends on whether escaped entities may appear in your text or not.

If there can be no escaped quotes,

Search: "[^"]*"(*SKIP)(?!)|_([A-Za-z])
Replace: \u$1

If there can be escaped quotes:

Search: "[^"\\]*(?:\\.[^"\\]*)*"(*SKIP)(?!)|_([A-Za-z])
Replace: \u$1

enter image description here

Details:

  • "[^"\\]*(?:\\.[^"\\]*)*"(*SKIP)(?!) - a "..." substring (it matches ", then any 0+ chars other than " and \ and then 0+ sequences of any escaped char (\\.) followed with any 0+ chars other than " and \, and then ") that is matched and then omitted from the match, and the next match is searched for after the end of the current match (due to (*SKIP)(?!))
  • | - or
  • _ - a _ char
  • ([A-Za-z]) - Capturing group 1: any ASCII letter

The \u operator in the replacement makes the first char after it uppercase. $1 stands for the letter captured with ([a-zA-Z]).

Upvotes: 2

leoinstack
leoinstack

Reputation: 26

With the following regex you match just the first _t

(?<=^job)_(\w)

so you could replace that with T.

See https://regex101.com/r/VcpAzt/1

Upvotes: 0

Belal Mohammed
Belal Mohammed

Reputation: 140

There is no need for regex in your case (press Ctr+H and replace all)

Replace

job_title <

with:

jobTitle <

Upvotes: 0

Related Questions