1

I'm trying to use flags within Google Forms, and I've been googling hoping to find an answer in the last couple of hours, but didn't find any. Google Forms say that the regular expression is not valid. Even when I use a simple regex such as: (?i)t. I'm trying to use the regex inside a paragraph question.

How can I make it work?

Edit:

What I really need is to match [a-zA-Z" ]+( *),( *)[1-9]([0-9]??)\n repeatedly, so each line will look something like: Sam "The Man" McAdams , 9\n. Of course, the number of lines is unknown. using the repetition modifiers of * or + at the end of the regex does not satisfy my needs, because if the first line is accepted as valid, the other lines might be composed of anything really, and it considers it as a valid input, while it's not.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
imminiman
  • 73
  • 5
  • Are you using the new Google Forms? – Wiktor Stribiżew Aug 11 '16 at 13:02
  • Yes, I am using the new Google Forms. – imminiman Aug 11 '16 at 14:13
  • I don't know what you're talking about since data validation does work with regex in the new version of Google Forms. For instance, this one works: `[a-zA-Z" ]+,[1-9]([0-9]??)\012` I just can't figure out how to use flags in it. – imminiman Aug 11 '16 at 15:21
  • By the way, I specifically need to use the multiline flag (?m) – imminiman Aug 11 '16 at 15:27
  • You can always workaround `(?m)` by using `\r?\n` or `(^|\r?\n)` / `($|\r?\n)` . – Wiktor Stribiżew Aug 11 '16 at 16:26
  • @WiktorStribiżew thanks for trying to help, but it seems you didn't get what the purpose of the multiline flag is. Quoting [Tim Pietzcker](http://stackoverflow.com/questions/3651725/match-multiline-text-using-regular-expression) - "MULTILINE or (?m) ... accept the anchors ^ and $ to match at the start and end of each line (otherwise they only match at the start/end of the entire string)." So, for example: `(?m)112$` would accept "**I love 112\n112 is 112**" but reject "**I love 112\n112 is the best!**" – imminiman Aug 11 '16 at 16:43
  • I know what it is for. It is redundant, you can always work around this modifier. What do you need to match? – Wiktor Stribiżew Aug 11 '16 at 16:48
  • @WiktorStribiżew `[a-zA-Z" ]+( *),( *)[1-9]([0-9]??)\n` repeatedly, so each line will look something like: **Sam "The Man" McAdams , 9\n**. Of course, the number of lines is unknown. using the repetition modifiers of * or + at the end of the regex does not satisfy my needs, because if the first line is accepted as valid, the other lines might be composed of anything really, and it considers it as a valid input, while it's not. – imminiman Aug 11 '16 at 17:00
  • Not sure this will work (due to the nested groups), but I guess what you need is [`^([a-zA-Z" ]+ *, *[1-9][0-9]?(\n|$))+$`](https://regex101.com/r/kQ9iI7/1) – Wiktor Stribiżew Aug 11 '16 at 17:05
  • Well, it's working like magic! I really like your wise solution. I'm sincerely grateful. Thank you! – imminiman Aug 11 '16 at 17:24

1 Answers1

2

You can use the following expression to validate an entire string that only consists of lines meeting your pattern:

^([a-zA-Z" ]+ *, *[1-9][0-9]?(\n|$))+$

See the regex demo.

The main point is to add an alternation group to match either a newline or the end of string ((\n|$)) and wrap the whole pattern into a +-quantified group ((...)+) anchored at both start (^) and end ($).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563