26

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..

MartyIX
  • 27,828
  • 29
  • 136
  • 207
tx31415
  • 263
  • 1
  • 3
  • 4
  • By the way, you should update the question to accurately reflect what you want. You seem to *not* want does but the question indicates you *do* want dots. – Tom Jan 20 '11 at 00:45

3 Answers3

55
^[^.]*$

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.

Bob Aman
  • 32,839
  • 9
  • 71
  • 95
6

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(".");
Tom
  • 21,468
  • 6
  • 39
  • 44
  • @tchrist - I updated my answer. I really have used libraries before (can't remember which though) that allow for exact matching. I figured the OP was having trouble with saying "not dot". Also -- I think my `contains` approach is pretty reasonable. But perhaps this is homework or the OP is forced to use some library that only accepts a regex... – Tom Jan 20 '11 at 01:10
  • I will add: my solution works for most languages, but there can be subtleties and I'm happy to try and clarify if the OP provides more details. Also, escaping in brackets is fine (in languages I have used) and I prefer to do it. – Tom Jan 20 '11 at 01:11
  • @Tom: I agree that a substring check instead of regex is a perfectly reasonable approach; the question sounds like homework or somehow in over their head. What you’re remembering is that Java has a screwed up `matches` method (don't get me started), which goes out of its way to supply anchors even when you don’t; that's just one of several reasons why you should only use its `find` method if you’re forced to use Java for regexes (it’s not very good at them). Most other regex languages aren't so lame. BTW, you still have a spurious backslash. Remove it and I'll remove my downvote. – tchrist Jan 20 '11 at 01:14
  • @tchrist: removed... but really? It does work. And `matches` is not what I'm thinking of. Now that I think of it, it was either a third party library or a utility function I wrote for myself. – Tom Jan 20 '11 at 01:21
  • @Tom I don’t like to see the extra backslashes in square-bracketed character classes because it risks people thinking that `\.` and `.` mean something different within them — and from there they’ll likely misextrapolate other dubious inaccuracies. It’s important that they realize things work differently *inside* the brackets than they work *outside* them. – tchrist Jan 20 '11 at 01:25
  • @tchrist Not sure I agree with your reasoning. I tend to prefer to escape the metacharacter inside the character class because most people *don't* know the behavior is different. With the escape character in place, the behavior is mentally unambiguous, even without this knowledge. It's a bit like including extra parens for readability even when order of operations would mean they're unneeded. – Bob Aman Jan 20 '11 at 01:28
1

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");
        }
Muhammad Mubashir
  • 1,591
  • 1
  • 21
  • 18