I have the following regex in the tool Obsidian-to-Anki Plugin I use, taken from their Wiki:
^Q: ((?:.+\n)*)\n*A: (.+(?:\n(?:^.{1,3}$|^.{4}(?<!<!--).*))*)
Source: Question answer style · Pseudonium/Obsidian_to_Anki Wiki https://github.com/Pseudonium/Obsidian_to_Anki/wiki/Question-answer-style
Currently, based on my limited knowledge of regex, this is how I interpret this regex:
Q:
to indicate the start of the Question. It captures from the next position till it sees the beginning of the AnswerA:
- when it sees a line break or a HTML comment which is put by the extension to denote AnkiID, it stops capturing.^.{1,3}$
- I don't understand what this is doing.So, given the following notes, it correctly matches the Question and Answers.
However, I want to make 2 changes to this regex:
---
. I want to capture the Answer all the way to this new line.Here is what I have experimented with at regex101 site. I got something working for the Question extraction, but for the answer I am not able to make it work as that part is too complex for me - any help would be appreciated. https://regex101.com/r/s15yb9/1
Here is an extract of my notes - the first 2 questions are already there in Anki, hence they have an ID. The last question is new.
Q: diff between null and undefined?
A: Null indicates an intentional empty value.
export function ticketStatus(tickets, ticketId) {
if (tickets[ticketId] === undefined) {
return 'unknown ticket id';
} else if (tickets[ticketId] === null) {
return 'not sold';
} else {
return `sold to ${tickets[ticketId]}`;
}
}
Q: rewrite to use [[JS Object.assign]]
visitor.ticketId = null;
return visitor;
A: Just pass only the props we want to overwrite to assign
return Object.assign(visitor, {ticketId: null});
Q: diff between isNaN() global vs Number.isNaN()?
A: global isNaN() does type conversion.
I am using Neuracache flashcard style and i had the same problem:
I came up with this, may be someone can find a better way to do it:
(?:<!---->\n)+([\s\S]*?) #flashcard ?\n*((?:\n(?:(?:```(?:[\s\S]*?)```)|^.{1,3}$|^.{4}(?<!<!--).*))+)
empty lines can be anywhere in question
empty lines can be in ``` ``` tags in Answer
HTML tags are allowed
give a empty line after an answer
Empty lines are allowed with minor caveats i think , Hope it helps to modify the other style
<!---->
Empty lines allowed anywhere in the question
End of Question #flashcard
Answer Starts on the next line or after any number of empty lines
```
import python
# Empty lines allowed only within the tags ``` in answer
```
answer ends when you give a empty line after this and outside ```
<!---->
Next question starts in the same format as above after an empty line
Every question should start with a <!---->
followed by a single newline:
(?:<!---->\n)+ # Match one or more occurrences of `<!---->` followed by a newline
([\s\S]*?) # Group 1 Capture any characters (including newlines) till the word ' #flashcard' is seen
#flashcard ? # Match the string `#flashcard`, optionally followed by a space
\n* # Match zero or more newline characters
( # Group 2
(?: # Start of a non-capturing group
\n # Match a newline character
(?:
(?:```(?:[\s\S]*?)```)| # Match a code block enclosed in triple backticks
^.{1,3}$| # Match a line with 1 to 3 characters (excluding newlines)
^.{4}(?<!<!--).* # Match a line with at least 4 characters (excluding newlines), excluding lines starting with `<!--`
)
)+ # End of the non-capturing group, match one or more occurrences
)
The two capture groups group 1 and group 2 match the answer and the question.
There is zero or one space allowed after #flashcard
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With