0

I need to determine if a string contains Invalid chars. The criteria is that it should only have [a-z], [A-Z], [0-9] and [/,.-\] characters and anything else is invalid. Also, chars followed by space followed by / followed by space followed by chars is valid. The regex I have below takes care of the first criteria but I am struggling with the second. I have tried many combinations, something like these, but no success.

public bool ContainsInvalidChars(string s)
{
    return new Regex(@"[^a-zA-Z0-9/,.-][@"\s/\s"]).IsMatch(s);
}

Examples of input strings:

below is Valid
CSU,USD,6230.67,705RA0 / 2YKMP,5/11/2020,5/11/2020,VM

below is invalid
CSU,USD,6230.67,705RA0 /2YKMP,5/11/2020,5/11/2020,VM

below is invalid
CSU, USD,6230.67,705RA0 / 2YKMP,5/11/2020,5/11/2020,VM

Any help is greatly appreciated. Thanks.

4
  • Your method does not return any value. You should use return Regex.IsMatch(s, @"pattern_here");. Commented May 14, 2020 at 13:29
  • Try return Regex.IsMatch(s, @"(?!\s/\s)(?<!\s/(?=\s))[^a-zA-Z0-9/,.-]"); Commented May 14, 2020 at 13:34
  • looks like X/Y for me. Imo use a well build CSV parser and trim the field. A space in front of a value should not be enough to make a line invalid. Commented May 14, 2020 at 13:34
  • Have you tried my approach? Commented May 14, 2020 at 15:42

3 Answers 3

1

You might use the character class and repeat 0+ times in a group starting with space / space and again the character class.

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

Regex demo | C# demo

The code could look like

public bool ContainsInvalidChars(string s)
{
    return new Regex(@"^[a-zA-Z0-9/,.-]+(?: / [a-zA-Z0-9/,.-]+)*$").IsMatch(s);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have two types of valid chunks:

[a-zA-Z0-9/,.-]
\s/\s

let's make an arbitrary combination of them (note anchors ^ and $ - we want entire string match)

^(?:(?:\s/\s)*[a-zA-Z0-9/,.-]*)*$

Finally, invalid means not matched:

Code:

 public static bool ContainsInvalidChars(string s) =>
   !Regex.IsMatch(s, @"^(?:(?:\s/\s)*[a-zA-Z0-9/,.-]*)*$"); 

Demo:

string[] tests = new string[] {
  @"CSU,USD,6230.67,705RA0 / 2YKMP,5/11/2020,5/11/2020,VM",
  @"CSU,USD,6230.67,705RA0 /2YKMP,5/11/2020,5/11/2020,VM",
  @"CSU, USD,6230.67,705RA0 / 2YKMP,5/11/2020,5/11/2020,VM",
};

string report = string.Join(Environment.NewLine, tests
  .Select(test => $"{test,-60} :: {(ContainsInvalidChars(test) ? "Invalid" : "Valid")}"));   

Console.Write(report);

Outcome:

CSU,USD,6230.67,705RA0 / 2YKMP,5/11/2020,5/11/2020,VM        :: Valid
CSU,USD,6230.67,705RA0 /2YKMP,5/11/2020,5/11/2020,VM         :: Invalid
CSU, USD,6230.67,705RA0 / 2YKMP,5/11/2020,5/11/2020,VM       :: Invalid

1 Comment

Thanks everyone. Learned something new about Regex from your responses.
0

I suggest using

public bool ContainsInvalidChars(string s)
{
    return Regex.IsMatch(s, @"(?!\s/\s)(?<!\s/(?=\s))[^a-zA-Z0-9/,.-]");
}

See the regex demo

Regex details

  • (?!\s/\s) - a location not immediately followed with whitespace + / + whitespace
  • (?<!\s/(?=\s)) - a location not immediately preceded with whitespace + / and then followed with whitespace
  • [^a-zA-Z0-9/,.-] - any char but an ASCII letter, digit, /, ,, . or -.

The two lookarounds just exclude matching whitespaces (that are otherwise matched with [^a-zA-Z0-9/,.-]) that are in a WHITESPACE + / + WHITESPACE chunk.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.