2

I have text like

question 1, chapter 1
...
solution for question 1, chapter 1
...
question 2, chapter 1
...

I am trying to figure out how to detect, via "VS Code search and replace" regex, questions that do not have a solution. I have tried variations of question (\d+), chapter (\d+)\n(.*\n+)solution for question (?!\1). The idea is I would match, if question 3 does not have a solution, question 3 when it would negate at "solution for question 4". This does not seem to work. I'm unsure why.

I have looked at a few other questions, but I can't figure it out with that information

luiscla27
  • 4,956
  • 37
  • 49
user760900
  • 298
  • 1
  • 12
  • Perhaps [question\s+(\d+),\s+chapter\s+(\d+)(?!.*question\s+\1\s+chapter\s+\2)](https://regex101.com/r/0oq0yd/1)? – Nick Aug 29 '23 at 05:47
  • @Nick - missed a comma in the initial question and that seems to break this formula – user760900 Aug 29 '23 at 05:56
  • @Nick - I tried that, but it seems to match [more than what we want](https://regex101.com/r/3pUI5p/1) – user760900 Aug 29 '23 at 05:59
  • Ah yeah... Is the text format fixed exactly? – Nick Aug 29 '23 at 06:01
  • @Nick yes it is – user760900 Aug 29 '23 at 06:27
  • 1
    A negative lookbehind for `solution for` at the beginning should work then https://regex101.com/r/5rkp1M/1 – Nick Aug 29 '23 at 06:47
  • 1
    Without crossing question or solution `^question\s+(\d+),\s+chapter\s+(\d+)(?!(?:\n(?!question |solution ).*)*\nsolution for question \1)` See https://regex101.com/r/FYV81X/1 – The fourth bird Aug 29 '23 at 07:35
  • @bohemian what does this have to do with VS Code? Or did you not actually put that there? https://meta.stackexchange.com/q/383668/997587 – starball Aug 29 '23 at 09:43
  • 1
    @starball there was a comment posted that identified the relevant tool/language, which is important/essential to know when answering regex questions, as "VS Code search and replace" and that was deleted as "no longer needed" due to that tag being added to the question. – Bohemian Aug 29 '23 at 16:33
  • Try `(?<!solution for )question\s+(\d+),\s+chapter\s+(\d+)\n\.\.\.\n(?!solution)`. Strictly for your input example. Or `(?<!solution for )question\s+(\d+),\s+chapter\s+(\d+)\n(\.\.\.)*\n(?!solution)` for a looser input. – Mark Aug 29 '23 at 17:18
  • Slightly better : `(?<!solution for )question\s+(\d+),\s+chapter\s+(\d+)(?!\n(\.\.\.)*\nsolution)` – Mark Aug 29 '23 at 17:28
  • @starball Editors differ in the regex engine they use. Even vscode's find in a file is different than a search across files in terms of allowable regex's. Vscode also has unique handling of newlines in its regex's. So knowing the editor is always helpful. – Mark Aug 29 '23 at 17:31
  • @Mark yep. I'm aware of that. I once wrote an answer post about it only to get the Q dup closed and the info moved to the closer's answer :P. I just didn't see the comment about this being a VS Code question. I think Bohemian deleted it before I could see it. I prefer when questions make clear how tags are relevant in an organic way if not obvious. Even still, the problem is there: are they writing a regex for find widget? or search view? they have different support for lookaround. – starball Aug 29 '23 at 20:47

1 Answers1

0

If you use the search box in an opened file, you might use a negative lookahead to not cross matching lines that start with either question or solution

^question\s+(\d+),\s+chapter\s+(\d+)(?!(?:\r?\n(?!question |solution ).*)*\r?\nsolution for question \1)

The pattern matches:

  • ^ Start of string
  • question\s+(\d+), match questions, then 1+ whitespace chars and capture 1+ digits in group 1
  • \s+chapter\s+ Match chapter between 1+ whitespace chars
  • (\d+) Capture 1+ digits in group 2
  • (?! Negative lookahead, assert what is to the right is not
    • (?:\r?\n(?!question |solution ).*)*
    • \r?\n Match a newline
    • solution for question \1 Match solution for question followed by the value captured in group 1 using a backreference \1
  • ) Close the lookahead

See a regex demo

enter image description here

The fourth bird
  • 154,723
  • 16
  • 55
  • 70