Hello everyone, regex newbie here, I could really use your help.
Consider the following examples:
AAA $ BBB $ CCC $ DDD $ EEE
AAA $$ FFF $$ BBB
I would like to come with a regex expression that matches $ BBB $, $ DDD $ and $$ FFF $$.
I thought that by adding the following paragraph to the “patterns” portion of my custom syntax I would get the correct matches for the first line:
{
“match”: “([\$])(?:(?=(\?))\2.)*?\1”,
“exclusive”: true,
“comment”: “Match text in between dollar signs”,
“captures”: {
“1”: {
“scope”: “color.accent04”
}
}
},
but when I try to test the syntax I get an error message saying that the syntax is invalid.
Moreover, I would have no clue how to further adapt this to get the correct match in the second line.
Any help would be greatly appreciated!
Below is an excerpt for italic and bold from one of the standard syntaxes. They should syntactic ally be quite similar to what you wqnt except for the difference in reserved character and the additional spaces you have applied to your delimiter (compared to these).
I would start with these and see if you can first get the syntax to recognise the dollar signs (note how the back slashes for escaping a character like an asterisk are themselves escaped ), then add your spaces in around the delimited content.
{
"match": "((?<![\\\\\\*_])(\\*|_)(?![\\\\\\*_])([\\S?].*?)(?<![\\\\|\\*])(\\2)(?![\\\\|\\*]))",
"exclusive": false,
"comment": "Markdown emphasis like *italic* or _italic_",
"captures": {
"2": {
"scope": "markup"
},
"3": {
"scope": "text.italic"
},
"4": {
"scope": "markup"
}
}
},
{
"match": "(((?<!\\\\)\\*\\*|__)([\\S?].*?)(\\2))",
"exclusive": false,
"comment": "Markdown bold like **bold** or __bold__",
"captures": {
"2": {
"scope": "text.boldMarker"
},
"3": {
"scope": "text.bold"
},
"4": {
"scope": "text.boldMarker"
}
}
},
Hopefully, that gets you on track and with some more rigorous reg ex courtesy of Greg.
Thank you so much, this was super helpful! Can’t believe I didn’t think of mimicking the structure for bold and italic Posting here my snippet of code for the record, in case anyone else finds it useful:
{
"match": "((?<![\\\\\\*\\$])(\\$)(?![\\\\\\*\\$])(.*?)(?<![\\\\|\\*])(\\2)(?![\\\\|\\*]))",
"exclusive": false,
"comment": "Match math inline mode",
"captures": {
"1": {
"scope": "color.accent04"
}
}
},
{
"match": "(((?<!\\\\)\\$\\$)(.*?)(\\2))",
"exclusive": true,
"comment": "Match math display mode",
"captures": {
"1": {
"scope": "color.accent04"
}
}
},
Note that I have removed the [\\S?]
because I was fine with having the match start with a space.
1 Like