1

For example I have the following string:

thats a\n\ntest\nwith multiline \n\nthings...

I tried to use the following code which does not work correctly and still hasn't all chars included:

string text = "thats a\n\ntest\nwith multiline \n\nthings and so on";
var res = Regex.IsMatch(text, @"^([a-zA-Z0-9äöüÄÖÜß\-|()[\]/%'<>_?!=,*. ':;#+\\])+$");
Console.WriteLine(res);

I want the regex returning true when only the following chars are included (do not have to contain all of them but at least one of the following and no others): a-z, A-Z, 0-9, äüöÄÖÜß and !#'-.:,; ^"§$%&/()=?\}][{³²°*+~'_<>|.

This is a list of known keyboard characters I thought of would be nice the use inside of a message.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
RoyBlunk
  • 91
  • 8
  • Not sure what you are looking for. Maybe this could help: https://dotnetfiddle.net/9IEDu0 – Rahul Sharma Feb 02 '22 at 16:58
  • I'm looking for a regex which can be used in my sample code to only return true when the given characters are used. If any other characters is used which is not included in my given list of characters it should return false. I have problems with escaping correctly. – RoyBlunk Feb 02 '22 at 18:36
  • Yes, there are still some characters left out but when they are included it should work also. – RoyBlunk Feb 02 '22 at 18:46
  • Yes because it will finde any substring in it but the whole string should match. – RoyBlunk Feb 02 '22 at 19:04
  • Your regex is basically what you need, but you test it against a string with line breaks. If the string must match, you need to include newlines to the character class. Look [here](https://regex101.com/r/WpSwkK/1), it matches after adding `\n`. Please update your question with your regex with all chars you need to allow in it to see what the exact issue is, and explain if newlines are also valid chars or not. – Wiktor Stribiżew Feb 02 '22 at 19:52
  • Also, your full regex declaration might look like ``@"^[a-zA-Z0-9äüöÄÖÜß!#'\-.:,; ^""§$%&/()=?\\}\][{³²°*+~'_<>|\n]+$"``, see [this demo](https://regex101.com/r/WpSwkK/2). – Wiktor Stribiżew Feb 02 '22 at 19:57

1 Answers1

2

If you specified all the chars you want to allow, the regex declaration in C# will look like

@"^[a-zA-Z0-9äüöÄÖÜß!#'\-.:,; ^""§$%&/()=?\\}\][{³²°*+~'_<>|]+$"

However, the test string you supplied contains line feed (LF, \n, \x0A) chars, so you need to either test on a string with no newlines, or add \n to the character class:

@"^[a-zA-Z0-9äüöÄÖÜß!#'\-.:,; ^""§$%&/()=?\\}\][{³²°*+~'_<>|\n]+$"

Note that the " char is doubled since this is the only way to put a double quote into a verbatim string literal.

Also, the capturing parentheses in your pattern create redundant overhead, you should remove them.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    This works very well, thank you! I didn't know of the double quotation marks and that it's possible to just write the linebreak into regex. Very interesting and helpfull answer. – RoyBlunk Feb 03 '22 at 07:04