Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 925
tx31415 Avatar asked Jan 20 '11 00:01

tx31415


People also ask

How would you match any character that is not a digit in regular expressions?

In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart. \d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ).

How do you match blank lines with regular expression?

The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string.

Does regex match dot space?

Yes, the dot regex matches whitespace characters when using Python's re module.

How do you match a character except one regex?

To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself. The character '. ' (period) is a metacharacter (it sometimes has a special meaning).


2 Answers

^[^.]*$ 

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.

like image 57
Bob Aman Avatar answered Dec 03 '22 01:12

Bob Aman


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("."); 
like image 23
Tom Avatar answered Dec 03 '22 01:12

Tom