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

Regex, match characters and doesn't contain more than one _ followed by another _
Regex ignore word
Regex not valid in google sheets error?
extract substring using regex in shell script
Eclipse Add text to first line of all files
Renaming files by using a prefix from text file
Python regex to find words, which also excludes particular words
Regex - exclude search results
Update a line in the AD info field
Sed Match Number followed by string and return Number
Perl matching string with mix of alphanumeric,_ and any number of square braces [closed]
Bash: extract the scheme and host part from a URL string
RexExp find a word within a list of words that are within a parenthesis
Converting EBNF to Regular Expression
Regex rule - allow empty field or special format [duplicate]
Using powershell to search for a pattern

Categories

HOME
validation
wix
bootstrap-4
jsonschema
asana
image-segmentation
tsql
openvpn
struts2-jquery
overloading
xorg
wavelet
obiee
eloqua
google-weather-api
axure
oracle-agile-plm
jsqmessagesviewcontroller
raspberry-pi2
round-robin
pywin32
media-source
django-autocomplete-light
cadvisor
sax
symbolic-math
flexjs
codewarrior
coqide
concurrenthashmap
nav
presto
jetty-9
qweb
runc
visualsvn-server
internet-explorer-10
xargs
maven-jetty-plugin
powerpc
hostapd
psycopg2
vega-lite
smartbanner
orchestrate
node.js-tape
canopen
harvest-scm
ietf-netconf
jce
ifc
hapi.js
xpdf
cufft
xmllint
dms
securesocial
groups
benerator
accounts
administrator
win32com
java-gstreamer
metalkit
membership
fab
linqtocsv
clipping
wchar-t
oboe.js
kindlegen
maven-cargo
swift-array
mks
sqlhelper
sendto
httpservice
xacml3
bson
youtrack-api
requiredfieldvalidator
xml.modify
srp
django-endless-pagination
couchrest
deap
ioerror
str-to-date
mips64
mms-gateway
google-goggles
osmf
alternate-data-stream
doophp
teamcity-7.0
arden-syntax
google-profiles-api
b-method
bubble-popup
ssao
xmltextwriter
orthographic
jdic
nstreecontroller
jquery-1.3.2
server.transfer
sharepoint-feature

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