开发者

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

开发者 https://www.devze.com 2023-02-05 09:24 出处:网络
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-31_4 but doesn\'t match: .swp, stackoverflow or test.开发者_运维问答

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-31_4 but doesn't match: .swp, stackoverflow or test.开发者_运维问答.


^[^.]*$

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.


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(".");


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

精彩评论

暂无评论...
验证码 换一张
取 消