regex


Why does this particular exclusionary regex of mine fail?


The following exclusionary regex should match (throw error on) only the chars in the brackets < > =
However, it also will throw on a space at the end of a string. Why?
/^((?![<>=]).)*$/
If it's a complete string, you don't need the look around. The problem is the look around needs a set number of characters in order to work (depending on the implementation meaning no wildcards usually).
If you're checking a whole string, you can use this:
/^[^<>=]+$/
If you're trying to make sure that doesn't occur at the beginning of the string, then you can use
/^[^<>=]/
End of the string
/[^<>=]$/
Beginning or End (not worrying about the middle) you should be able to use this.
/(^[^<>=]|[^<>=]$)/
If we test these in PHP using the following code:
<?php
$phrases = array('dog','cat=dog','<a href=','bird=');
$patterns = array('/^[^<>=]+$/','/^[^<>=]/','/[^<>=]$/','/(^[^<>=].*[^<>=]$)/');
foreach($phrases as $phrase){
foreach($patterns as $pattern){
if(preg_match($pattern,$phrase)){
echo '`'.$pattern."` matches `".$phrase."`\n";
} else {
echo '`'.$pattern."` doesn't match `".$phrase."`\n";
}
}
}
?>
We get the following output
/^[^<>=]+$/ matches dog
/^[^<>=]/ matches dog
/[^<>=]$/ matches dog
/(^[^<>=].*[^<>=]$)/ matches dog
/^[^<>=]+$/ doesn't match cat=dog
/^[^<>=]/ matches cat=dog
/[^<>=]$/ matches cat=dog
/(^[^<>=].*[^<>=]$)/ matches cat=dog
/^[^<>=]+$/ doesn't match <a href=
/^[^<>=]/ doesn't match <a href=
/[^<>=]$/ doesn't match <a href=
/(^[^<>=].*[^<>=]$)/ doesn't match <a href=
/^[^<>=]+$/ doesn't match bird=
/^[^<>=]/ matches bird=
/[^<>=]$/ doesn't match bird=
/(^[^<>=].*[^<>=]$)/ doesn't match bird=

Related Links

Perl change number to words
regex to match value up to 2 decimal
What is the mappings.ts file and how should it be set up in Tritium?
Symfony2 IBAN Validator returns false for valid UK IBAN
Regex to find anchor tags which are without http or https in the href attribute
What's the difference between [:space:] and [:blank:]?
Regex to replace &nbsp with
How do I replace one or more whitespace characters using the replace() function in XQuery?
.htaccess Pretty URL not displaying correctly with redirection
regex replacing several special characters
Creating a delimited text using regex
Perl regex with a negative lookahead behaves unexpectedly
Regex Get path name from full path
Replacing comma's by dots in floats using regular expressions
Visual Studio 2012 Regexes
regEx search/replace variable name without preceeding “_”

Categories

HOME
printing
lua
isis
typeahead.js
wso2is
wikipedia-api
payment-processing
google-adwords
clion
uwsgi
union
calabash-android
graphcool
nsmutableattributedstring
teamcity-9.0
nested-set-model
semantics
microphone
csom
py4j
shape
padding
weather-api
feedly
dapper-fastcrud
concurrenthashmap
json-c
angular2-databinding
svgpanzoom
machine-language
spring-websocket
outlook-vba
api-doc
titanium-alloy
macvim
transpose
cookiestore
caldroid
bem
essbase
php-deployer
mobile-browser
slt
ganglia
libev
g-wan
view-source
manual
column-oriented
a-star
swift-package-manager
getelementbyid
callkit
zenity
lapply
wxhaskell
xceed
webclient-download
dms
bash-completion
mutation
yapdatabase
cucumber-java
membership
armv7
laravel-forge
sample-data
hypervisor
treegrid
pegkit
emberfire
gitattributes
puppet-enterprise
kue
modern-ui
culture
knockout-mvc
jet.com-apis
epoll
keymapping
validform
antisamy
pydatalog
blueprint
ducttape
ccombobox
webcal
source-highlighting
nspersistentdocument
haar-wavelet
gigaspaces
clean-urls
documentviewer
xml-dtd
ms-access-97
cellphone
semantic-zoom
springboard
soapheader
emacs-ecb
treetop
unitils
gedcom
clipboard-pictures
newsgroup

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App