0

I'm trying to do a custom markdown notation and I need the inner most markdown every time, At the moment I have only 2 markdowns for testing purposes _ and *

Regex: (?<![_\*])(?<!\\)\*(?![_\*])(([^\*]|\\\*)*)(?<![_\*])(?<!\\)\* for capturing the inner most *

Examples:

  1. Should capture: *test1\*test2* - Captures!
  2. Should capture: _*test1\*test2*_ - but doesn't Capture because of the next rule.
  3. Shouldn't capture: *_test1\*test2_* some random text * - Doesn't Capture *_test1\*test2_* and captures * some random text * but I want to ignore this capture.

If I remove my first look before (?<![_\*]) works for 1 and 2, but captures the last * from the text until the next one because the beginning * is from from the previous text which should be ignored

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
T. Razvan
  • 21
  • 3
  • Can you be more specific about what rules your regex needs to enforce? – Yftach Aug 19 '21 at 13:56
  • Is "parse Markdown with Regex" the new "[parse HTML with Regex](https://stackoverflow.com/a/1732454/121493)"? – Donut Aug 19 '21 at 13:57
  • I want to capture the inner most markdown and ignoring the escaped ones. But if it's not the inner most I want to ignore the last markdown as well I've edited the post and tried to explain it better. – T. Razvan Aug 19 '21 at 14:10
  • Maybe you want ``\*(?!\s)([^\\*_\n]*(?:\\.[^\\*_\n]*)*)(?<!\s)\*``? See https://regex101.com/r/JpGhAW/1 – Wiktor Stribiżew Aug 19 '21 at 14:40
  • Yes, that's good but I doesn't work with `* test *`, but I think I can work with this. – T. Razvan Aug 19 '21 at 14:53
  • I just realized that I actually need the outer most markdown, not the inner most :( – T. Razvan Aug 19 '21 at 15:01
  • I think that's it `(?<!\\)(?<![_\*])\*(?!\s)([^\\\*\n]+(?:\\.[^\\\*\n]*)*)\*(?![_\*])`. Thanks for your help. – T. Razvan Aug 19 '21 at 15:17

0 Answers0