How to find a text between two characters and replace it by a text between another two characters of a line in notepad++?

馋奶兔 提交于 2019-12-31 04:08:13

问题


I have many lines of bibliography as the following:

@article{mehri_buckling_2016, title = {Buckling }, doi = {10.1016/j.cma.2016.01.017}}

@article{jin_modified_2014, title = {A modified ns}, doi = {10.1016/j.apacoust.2014.04.007}}

@article{sofiyev_free_2017, title = {The free}, doi = {10.1016/j.compositesb.2017.03.054}}

@article{malekzadeh_three-dimensional_2012, title = {Three-dimensional free }}

@article{jooybar_thermal_2016, title = {Thermal panels}, doi = {10.1016/j.tws.2016.01.032}}

In each line may there is doi={...} or not. I need to replace the words between @article{......, by the word between doi = {...}, if there is doi in its line.

For this example results should become:

@article{10.1016/j.cma.2016.01.017, title = {Buckling }, doi = {10.1016/j.cma.2016.01.017}}

@article{10.1016/j.apacoust.2014.04.007, title = {A modified ns}, doi = {10.1016/j.apacoust.2014.04.007}}

@article{10.1016/j.compositesb.2017.03.054, title = {The free}, doi = {10.1016/j.compositesb.2017.03.054}}

@article{malekzadeh_three-dimensional_2012, title = {Three-dimensional free }}

@article{10.1016/j.tws.2016.01.032, title = {Thermal panels}, doi = {10.1016/j.tws.2016.01.032}}

回答1:


You could do it like this:

  • Find what: ^(@article\{).*?(,.*?doi = \{)(.*?)\}
  • Replace with: \1\3\2\3}
  • Regular expression: checked
  • . matches newline: cleared

Explanation:

  • ^ is maybe optional, but it ensures that the match can only occur at the beginning of a line.
  • (@article\{) matches literally @article{. The brace had to be escaped with a backslash, as it is a special character in regexes. The parentheses make it possible to refer to this string with \1 later. It's a so called "capture group"
  • .*? will match any series of characters , but not more than necessary to get a complete match ("lazy"): . means any character, * any number of them, ? but as few as possible to make the rest work.
  • (,.*?doi = \{) matches a literal comma, then again some more characters (see previous point), followed by a literal doi = {. The parentheses make it possible to refer to this as \2 later.
  • (.*?) matches a number of characters (see above). The parentheses make it possible to refer to this as \3 later. This is the part that matches the actual text that needs to be copied.
  • \} a literal }.

The replacement uses the capture groups:

  • \1 is synonymous for @article{
  • \3 is the text that needs to be copied
  • \2 is whatever appeared between , and doi = {, including those two texts.
  • \3 is repeated to just restore the doi value as it was.
  • } is the closing brace that was matched at the end of the "find" pattern. We want to keep it as well of course.


来源:https://stackoverflow.com/questions/59490327/how-to-find-a-text-between-two-characters-and-replace-it-by-a-text-between-anoth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!