Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Update regex to be lazy and handle ! in text #1

Merged
merged 2 commits into from
Jan 10, 2023

Conversation

jeremyfelder
Copy link

Description

This PR reverts the regex to use a match all (.+) token with the addition of being lazy (? - match as little as possible, consuming as needed) instead of greedy (match as much as possible, giving back as needed) which allows for using an ! in the text.

Regex Explanation

[!]{2} - match two leading exclamation points
(?P<class>.+?)[|^] - match anything once or more (lazily) until a | or ^ is found
(?P<text>.+?(?=[!]{2})) - match anything once or more (lazily) until two consecutive !s are found
(?=(?P<lookahead>[!]{3}))? - check if there are three consecutive !s
(?(lookahead)(?P<exclaim>!)) - if three consecutive !s were found match one !
[!]{2} - match two trailing exclamation points

Tested Cases

  • multiple spans in a single line
  • using an ! anywhere in the text
  • using an ! anywhere in the class

NOTE: The edge case of an exclamation point at the end of the text (i.e. having 3 ! in a row) required appending an ! to the text

See here for tested cases: https://regex101.com/r/BZnHIO/2

NOTE:
Markdown is using the python standard library re as its regex library. re doesn't allow for conditional expressions unless the condition is a previous capture group - https://docs.python.org/3/library/re.html?highlight=(?(id/name). This forced a conditional matching capture group named "lookahead" which is then used in the conditional expression instead of having the lookahead inline in the conditional.

@wilhelmer
Copy link
Owner

Looks good to me, thanks for contributing!

@wilhelmer wilhelmer merged commit 8b2793d into wilhelmer:master Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants