'Validating MS Teams channel names, team name and channel address in one go //OwMyHead

Validating MS Teams channel names, team name and channel address in one go!

Regex1:

^(?![\s._])([^~#%&*{}+:<>?|\n]{1,50})(?<![.])( - ).{1,256}$

Which would validate this successfully:

MonkeyChannel  - MonkeyTeam

but I need to check also that it doesn't contain the channel address like so:

MonkeyChannel  - MonkeyTeam <[email protected]>

so basically I'm thinking I need to incorporate this which looks for a channel address:

Regex2:

(?<![[a-z0-9]{8}\.domain\.com@emea\.teams\.ms])

into this somehow:

Regex1:

^(?![\s._])([^~#%&*{}+:<>?|\n]{1,50})(?<![.])( - ).{1,256}$

My guess looks something like this but its not working:

Regex3:

^(?![\s._])([^~#%&*{}+:<>?|\n]{1,50})(?<![.])( - ).{1,256}(?<![[a-z0-9]{8}\.domain\.com@emea\.teams\.ms])$

Any regex wizards who can spot the error of my ways?



Solution 1:[1]

You could either add the negative lookbhind after the anchor and note that you have <...> and not [...]

 ^(?![\s._])[^~#%&*{}+:<>?|\n]{1,50}(?<![.]) - .{1,256}$(?<!<[a-z0-9]{8}\.domain\.com@emea\.teams\.ms>)

Regex demo

The other way around is using a negative lookahead after matching -

^(?![\s._])[^~#%&*{}+:<>?|\n]{1,50}(?<![.]) - (?!.*<[a-z0-9]{8}\.domain\.com@emea\.teams\.ms>).{1,256}$

Regex demo

You can add the capture group accoringly if you want to after process separate parts.

If you still want to match the first part, you can optionally match the second and first assert that it does not start with the unwanted mail address

^(?![\s._])[^~#%&*{}+:<>?|\n]{1,50}(?<![.]) - [^<>\n]*(?:<(?![a-z0-9]{8}\.domain\.com@emea\.teams\.ms>).{1,256})?

Regex demo

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1