regex


Regex - unknown place of character


I have a string like as abcd but the place of a is unknown. The string can be bacd or bcad or bcda.
I tried
a{0,1}ba{0,1}ca{0,1}da{0,1}
but this pattern matches bcd too.
I need exactly one occurrence of a with unknown place.
a is a symbol. Iwant to use sth like as b[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f] Instead of a.
Short answer
abcd|bacd|bcad|bcda
Perfectly readable, good performance, and works everywhere.
Longer answer.
Regular expression should not be used too hackly. It is a state machine and will run slow if you make it too complex.
When time you need to while a small function, write that function.
a?ba?ca?da? -- with a following up function to count the number of a.
Your criteria seem to be:
bcd all appear once and in that order
a appears once anywhere (and "a" is a placeholder for a complex expression)
no other chars
This will do it:
^(?!bcd$)(?!(.*a){2})(a)?b(a)?c(a)?d(a)?$
See live demo.
The look-aheads ensure there is exactly one a, and "a" can be a complete expression.
Note also the use of ? instead of {0,1}.
This answer builds on Dennis Cheung's answer, and is written in java (You did not specify a language)
string.replaceAll("a", "").matches("bcd") &&
string.replaceAll("[^a]", "").equals("a") &&
string.matches("a?ba?ca?da?")

Related Links

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 “_”
Perl regex script and command line different
Combine Multiple Regexp Patterns
Regular expression for alphamumeric value and can contain hyphen
Regex to determine if string contains all the vowels? [closed]
Regex matching “a > b > c” pattern?

Categories

HOME
wpf
ffmpeg
wix
gwt
android-sqlite
haskell-stack
word-vba
styling
bootstrap-selectpicker
stack
weblogic11g
addeventlistener
telnet
wso2ei
executable
marketplace
nservicebus
jquery-jtable
numericupdown
plc
google-finance
game-maker-language
nxbre
jpos
django-autocomplete-light
datamatrix
background-subtraction
iteration
android-button
ucwa
monads
expression-trees
laravel-valet
svn2git
cloudinary
ollydbg
server-sent-events
sieve-of-eratosthenes
phing
draggable
ecmascript-4
packaging
microsoft-translator
protobuf-3
wso2iot
automata-theory
php-5.5
sdcc
virtualmin
multistore
jawr
flying-saucer
graph-traversal
bosh-deployer
mysql-error-1050
kie-workbench
ubuntu-15.04
vb4android
cglib
menustrip
geneos
submission
jstorm
vb6-migration
uploadify
google-api-ruby-client
jedi
dbfit
interpreted-language
text-alignment
session-replication
axlsx
cbc-mode
alienvault
data-science-studio
alt
fortrabbit
epplus-4
scribe
tortoisecvs
servlet-listeners
typesafe-activator
imaging
master-theorem
sirtrevor
b2b
windows-phone-7.1
ramdisk
onejar
re2c
rte
datapump
hla
mosek
godaddy-api
hogan.js
alternate-data-stream
cloud-hosting
android-actionmode
graphiti-js
hpple
httpconnection
payment-services
junitperf
jetspeed2
openfeint
rails-3.1
excel-web-query
xtratreelist
dynamic-websites
shareware
metaphone
docking
roguelike

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