简体   繁体   中英

Regex match string or end of string

I currently have this regex (with multiline and dotall flags):

^@@:([a-zA-Z0-9_-]*)\s*^(.*?)^@@

That matches this:

@@:variable

some nice MarkDown content blah blah

@@

I would like to be able to omit the closing @@ tag if the declaration is the last in the file (string), this is because many of the files will only have one declaration so the idea of closing it seems unnecessary. I tried this regex:

^@@:([a-zA-Z0-9_-]*)\s*^(.*?)^(@@|\A)

But it no longer even matches with the closing tag with that.

Thanks in advance for your help.

I have got it now:

Two mistakes:

The caret had to be part of the first regex in the last group and I needed \\Z not \\A as \\A matches start of string rather than end.

^@@:([a-zA-Z0-9_-]*)\s*^(.*?)(^@@|\Z)

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