Woww
Woww

Reputation: 374

Regex - Match each line or nothing

I have this /([^\s](\-|\w)+\s(\d){1,2}[^\w\d\s])+/s regex-expression in order to match this:

Name 11
Name-with-Seperators 22
Another Name 33

I want to validate that the input follows this format: Name, white-space, number, line-break

If it does not, it should not match. However I can only make it work to match something. The input validation happens in Google Sheets, so I cannot split each line and test it separately easily.

How can I achieve this?

Upvotes: 0

Views: 177

Answers (1)

kibotrel
kibotrel

Reputation: 26

If your goal is to match every line separately : /^[a-z\-]+\s\d+$/gim

it matches at first, a group of letter with potential separators, then looks for a whitespace and a group of digits.

  • g modifier is for the regex not to stop at the first match
  • i modifier is for case insensitiveness
  • m modifier is for multiline input

But if your goal is to match the whole thing as only one group that's either true or false you could go for something like : /^[a-z\-]+\s\d+(\n[a-z\-]+\s\d+)*\n$/i

It assumes that each line even the last is ended by a newline. It does the same thing as the first one but after the first line it checks for between 0 and unlimited lines that follow the same pattern as the first one.

Upvotes: 1

Related Questions