1

I'm trying to develop regular expressions to check syntax for question writing for a quiz module.

One question type is drag and drop which has the format ...

the {rain} falls gently on the {plain}

... where the bracketed terms are draggable. There has to be at least one bracketed term but they can come anywhere in the string. The doesn't need to be anything between the bracketed terms either. So any of the following are valid ...

{the} rain falls gently on the plain

the rain falls gently on the {plain}

{the} {rain} {falls} {gently} {on} {the} {plain}

{the }{rain }{falls }{gently }{on }{the }{plain}

Also, to complicate matters, there can't be any square brackets inside the braces - this is another question type. So ...

{the} {rain}

... is OK, but ...

{the} {[rain]}

... is not and any other combination.

So far, I've got this but it clearly doesn't work :(

(.*\{.*\})+

Please can someone help me out?

3
  • This may do the trick regex101.com/r/k2BTpu/2 Commented Feb 10, 2020 at 21:11
  • Can there be { and } between the {}? Commented Feb 10, 2020 at 21:24
  • Yes it very well can be. Commented Feb 10, 2020 at 21:27

1 Answer 1

1

If { and } can not occur between the {} and the square brackets can be present outside of the curly braces but not inside, you might use:

^[^\r\n{}]*(?:{[^\][\r\n{}]*}[^\r\n{}]*)+$
  • ^ Start of string
  • [^\r\n{}]* Repeat 0+ times any char except a newline or { and }
  • (?: Non capture group
    • {[^\][\r\n{}]*} Match {, then 0+ times any char except a newline or any of []{}
    • [^\r\n{}]* Match 0+ times any following chars except a newline or { and }
  • )+ Close group and repeat at least 1 or more times to make sure there is a draggable {...}
  • $ End of string

Regex demo

Sign up to request clarification or add additional context in comments.

9 Comments

Yep - thank you - that seems to work perfectly. I shall study it and learn more :)
After studying this a little, do you never need to specify "any character" as a dot at any point in this? So is matching any character explicit in RegEx? Sorry for not getting this :(
It is not mandatory to use a dot to write a pattern. In this pattern the negated character classes specify what you want to allow to match except what is listed.
Right - so this could be the issue with this - I can't get the string to match without the .+ being present but then it matches everything after it, even if the syntax is wrong. regex101.com/r/oHz9P8/1 I guess I need to use capturing groups? The correct syntax must be ... {[answer][yes]}{[answer][no]}{[answer][no]}{[answer][no]} There can only be one 'yes' and as many 'no' as required (there is no limit on options in the question) and the 'yes' and 'no's can be anywhere in the string. Phew - this is harder than I thought.
@MrMills The .+ is a very broad match and will match until the end of the string first and will then backtrack trying to fit the rest of the pattern. You might do it like this by repeating the no patterns and match at least a single yes pattern regex101.com/r/JKJiYb/1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.