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}}
You could do it like this:
^(@article\{).*?(,.*?doi = \{)(.*?)\}
\1\3\2\3}
.
matches newline: clearedExplanation:
^
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 literaldoi = {
. 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,
anddoi = {
, including those two texts.\3
is repeated to just restore thedoi
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.