How to match only strings that do not contain a dot (using regular expressions)

后端 未结 3 1367
感情败类
感情败类 2020-11-30 08:58

I\'m trying to find a regexp that only matches strings if they don\'t contain a dot, e.g. it matches stackoverflow, 42abc47 or a-bc-

相关标签:
3条回答
  • 2020-11-30 09:13

    Use a regex that doesn't have any dots:

    ^[^.]*$
    

    That is zero or more characters that are not dots in the whole string. Some regex libraries I have used in the past had ways of getting an exact match. In that case you don't need the ^ and $. Having a language in your question would help.

    By the way, you don't have to use a regex. In java you could say:

    !someString.contains(".");
    
    0 讨论(0)
  • 2020-11-30 09:25
    ^[^.]*$
    

    or

    ^[^.]+$
    

    Depending on whether you want to match empty string. Some applications may implicitly supply the ^ and $, in which case they'd be unnecessary. For example: the HTML5 input element's pattern attribute.

    You can find a lot more great information on the regular-expressions.info site.

    0 讨论(0)
  • 2020-11-30 09:35

    Validation Require: First Character must be Letter and then Dot '.' is not allowed in Target String.

    // The input string we are using string input = "1A_aaA";

            // The regular expression we use to match
            Regex r1 = new Regex("^[A-Za-z][^.]*$"); //[\t\0x0020] tab and spaces.
    
            // Match the input and write results
            Match match = r1.Match(input);
            if (match.Success)
            {
                Console.WriteLine("Valid: {0}", match.Value);
    
            }
            else
            {
                Console.WriteLine("Not Match");
            }
    
    0 讨论(0)
提交回复
热议问题