2

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:

  1. Question starts on a new line with Q: to indicate the start of the Question. It captures from the next position till it sees the beginning of the Answer
  2. Answer starts on a new line with A: - when it sees a line break or a HTML comment which is put by the extension to denote AnkiID, it stops capturing.
  3. I do not really understand the check for a line that contains ^.{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:

  1. I want to allow empty lines in both question and answer - I often put code in the question and answer. When there is an empty line in the code, the question or the answer gets cuts off
  2. Each Question and Answer section always ends with a new line ---. 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.

  • Undefined indicates the total absence of a value. This happen when only the variable was declared without any initializer. A missing key in an object is also undefined.
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.

  • Since I am trying to test whether the string contains a valid number, this is the right choice.
  • If the input is already a number, then Number.isNaN() might be better. as

Siraj Samsudeen
  • 1,624
  • 7
  • 26
  • 35
  • i have similar problem and i just found out this should be OK to use for new lines `((?:[^\n][\n]?)+) #flashcard ?\n*((?:\n(?:^.{1,3}$|^.{4}(?<! – munish Jun 29 '23 at 13:48

1 Answers1

1

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

munish
  • 4,505
  • 14
  • 53
  • 83
  • this is great. Can you please provide some explanation of the regex - it has been a while since I touched this and would be grateful if you can provide your thought process on how you broke the problem down. – Siraj Samsudeen Jul 04 '23 at 03:38
  • i don't understand ^.{1,3}$ either but i am just keeping – munish Jul 04 '23 at 18:54
  • you can still add empty newline looking paragraph by just hitting a couple of spaces on the newline – munish Jul 05 '23 at 12:15