2

I'm trying to validate text in a text area. (I will eventually be using an Angular 9 pattern validator with this regex). Can you help me with the regex?

Text will be 1 to N pairs of alphanumeric characters separated by commas, with new lines at the end of each pair. Spaces before or after each alphanumeric don't matter.

Example data (I don't know how to represent the newline in the blockquotes):

TEST12345, R5045876
EUDONOT1234, EF2345Y
AB345EEI2,FT0123

Here's what I have tried for Regex in RegExr which seems to pass, but I can't seem to get it to work as my pattern string.

[ ]?[a-zAZ0-9](?:,[a-zA-Z0-9])*$

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Jennifer S
  • 1,419
  • 1
  • 24
  • 43
  • 1
    Do you mean there can be any amount of comma-separated alphanumeric words on one or more lines? `^[a-zA-Z0-9]+(?:,[^\S\n]*[a-zA-Z0-9]+)*(?:\n[a-zA-Z0-9]+(?:,[^\S\n]*[a-zA-Z0-9]+)*)*$`? https://regex101.com/r/GCxssg/1/ – Wiktor Stribiżew Dec 29 '20 at 21:40
  • Yes, there can be any number of comma separated alphanumeric words, with newline characters between pairs. – Jennifer S Dec 29 '20 at 21:46
  • 1
    If you mean there can be only two words per line, you need to remove `(?:` and `)*`: `^[a-zA-Z0-9]+,[^\S\n]*[a-zA-Z0-9]+(?:\n[a-zA-Z0-9]+,[^\S\n]*[a-zA-Z0-9]+)*$`, see https://regex101.com/r/GCxssg/2 – Wiktor Stribiżew Dec 29 '20 at 21:47

2 Answers2

1

You can use

/^[a-zA-Z0-9]+,[^\S\n]*[a-zA-Z0-9]+(?:\n[a-zA-Z0-9]+,[^\S\n]*[a-zA-Z0-9]+)*$/

See the regex demo.

Details:

  • ^ - start of string
  • [a-zA-Z0-9]+ - one or more alphanumeric chars
  • , - a comma
  • [^\S\n]* - zero or more whitespace chars other than a line feed char
  • [a-zA-Z0-9]+ - one or more alphanumeric chars
  • (?: - start of a non-capturing group matching
    • \n - a newline, line feed char
    • [a-zA-Z0-9]+,[^\S\n]*[a-zA-Z0-9]+ - same as above, a word, comma, optional whitespace other than LF, and a word
  • )* - end of the group, one or more repetitions
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Special thanks to Wiktor, for his help in the comments, and his previous answer in this Angular Pattern answer which reminded me what I had forgotten. :) He really deserves some points for this.

His second regex example in the comment was the perfect answer, and then, since I'm using strings to give the patterns regular names, here's my pattern:

private multilinePairsCommaDelimitedWords = '[a-zA-Z0-9]+,[^\\S\\n]*[a-zA-Z0-9]+(?:\\n[a-zA-Z0-9]+,[^\\S\\n]*[a-zA-Z0-9]+)*';

and the final use of my validator:

this.firstFormGroup = this._formBuilder.group({
  workstations: ['', [Validators.required, Validators.pattern(this.multilinePairsCommaDelimitedWords)] ],
  softDeleteDate: ['', Validators.required]
});
Jennifer S
  • 1,419
  • 1
  • 24
  • 43
  • No, do not use the string pattern as you see for yourself you have to mess with slashes. Just use the *regex literal notation* as shown in my answer: `private multilinePairsCommaDelimitedWords = /^[a-zA-Z0-9]+,[^\S\n]*[a-zA-Z0-9]+(?:\n[a-zA-Z0-9]+,[^\S\n]*[a-zA-Z0-9]+)*$/` (in this case, the `^` and `$` anchors are required though, in the case of a string pattern, they are implicit). – Wiktor Stribiżew Dec 29 '20 at 22:12
  • OK. I will try that, also. The string version I'm using does work. Thanks again! – Jennifer S Dec 29 '20 at 22:14
  • 1
    And yes, what Wiktor shows in the above comment is working. Hooray! Thanks for teaching me something else today. – Jennifer S Dec 29 '20 at 22:20