Regex to Match a String Not Starting or Ending With a Pattern

Regex that checks that a string should not start or end with a space and should not end with a dot (.)

Use following regex

^\S.*[^.\s]$

Regex explanation here

Regular expression visualization


If you want to match single character then you can use look-ahead and look behind-assertion.

^(?=\S).+(?<=[^.\s])$

Regex explanation here

Regular expression visualization


If look-behind not supports then use

^(?=\S).*[^.\s]$

Regex explanation here

Regular expression visualization

RegExp matching string not starting with my

You could either use a lookahead assertion like others have suggested. Or, if you just want to use basic regular expression syntax:

^(.?$|[^m].+|m[^y].*)

This matches strings that are either zero or one characters long (^.?$) and thus can not be my. Or strings with two or more characters where when the first character is not an m any more characters may follow (^[^m].+); or if the first character is a m it must not be followed by a y (^m[^y]).

Match strings which do not start or end with

Your last negative should be a look behind not a look ahead, At the point of the test you've already read the .js

^(?!files\/).*(?<!\.js)$

Regular expression for words not starting or ending with vowels?

select distinct(city) from station
WHERE regexp_like(city, '^[^aeiou](\w|\s)*$', 'i')
OR regexp_like(city, '^(\w|\s)*[^aeiou]$', 'i');

The question is asking "ends with OR starts with" you can do it in the regex, but I did it as an OR in the WHERE clause

Regex for string not ending with given suffix

You don't give us the language, but if your regex flavour support look behind assertion, this is what you need:

.*(?<!a)$

(?<!a) is a negated lookbehind assertion that ensures, that before the end of the string (or row with m modifier), there is not the character "a".

See it here on Regexr

You can also easily extend this with other characters, since this checking for the string and isn't a character class.

.*(?<!ab)$

This would match anything that does not end with "ab", see it on Regexr

regex to match string not starting and/or ending with spaces but allowing inbetween spaces

This pattern should do the trick:

^\s*.*?\s*$

And usage:

Regex.IsMatch(input, @"^\s*.*?\s*$");

Regex match string not starting with

If you just try to match a string that does start with example but not with www.example than it would be as easy as:

^example

Otherwise you can use a negative lookbehind:

(?<!\bwww\.)\bexample\b

The \b in here is a word boundery, meaning that it matches it, as long as it is not part of a string. (In other words if it isn't followed by or following any A-Za-z characters, a number or an underscore.)

As mentioned by @CasimiretHippolyte this does still match strings like www.othersubdomain.example.com.

Example



Related Topics



Leave a reply



Submit