简体   繁体   中英

In regex, match specific string pattern but omit lines that are commented out

We are using regex with the sublime text find & replace tool to change strings across many files. The strings are references to database tables and are in the form dbname.dbschema.dbtable . For example, we have a file that may look like:

select * from mydb.myschema.first_table
anotherdb.secondschema.first_table
anotherdb.secondschema.first_table
-- reportsdb.schema.first_table
-- select * from words.potato.first_table

We then want our regex search to find all cases in these files where the string ends in first_table , and is preceded by anyword.anyword. . We have the following regex \w+\.\w+\.(first_table)+(\w+)? and it does find all 5 instances and selects the string as needed:

在此处输入图像描述

However, we want to omit lines that are commented out, so any lines that start with - , and this is the part I am struggling with. Is it possible to update our regex search to omit lines that start with a - ?

In Sublime Text, you may use

^\s*--.*(*SKIP)(*F)|\w+\.\w+\.first_table$

See the regex demo .

Details

  • ^\s*--.*(*SKIP)(*F) - start of a line, zero or more whitespaces, -- and then the rest of a line are matched and skipped with the help of the (*SKIP)(*F) verbs, and the next match is searched for from this location where the match got skipped
  • | - or
  • \w+\.\w+\.first_table$ - one or more word chars, . , one or more word chars, then first_table and the end of a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM