Home > Mobile >  Regex to match a bibkey that contains & as part of the text to be matched
Regex to match a bibkey that contains & as part of the text to be matched

Time:10-17

I am working on a LaTeX project that contains a bibliography file (.bib) the contents of which are as follows:

@Article{abc123,

@InBook{def123&233,

amongst others. So, the specification of the expression that I would like to match are those lines that start with @ followed by a sequence of characters (Article or InBook in the example above) followed by { followed by a sequence of characters, numbers and everything else possible except a , terminated by a , (this terminating , should be matched)

So, in the example above, I would like to have a regexp that matches:

@Article{abc123, and @InBook{def123&233,

Using the online regexp generator, I obtained:

@[A-Za-z0-9] \{[A-Za-z0-9] &[A-Za-z0-9] ,

but this regexp matches @InBook{def123&233, but does not match @Article{abc123, because the latter does not contain &.

How can I create a regexp that considers & as another ordinary character with no special meaning?

CodePudding user response:

You may use this regex:

@[A-Za-z0-9] \{[^,] (?:,|\s*$)

RegEx Demo

RegEx Details:

  • @: Match a @
  • [A-Za-z0-9] : Match 1 of alphanumeric characters
  • \{: Match a {
  • [^,] : Match 1 of any character that is not comma
  • (?:,|\s*$): Match comma or end of string with optional trailing spaces
  • Related