regex


Regular expression to split optional groups


Full string syntax is: "db:server:port"
Server and port are optional, i.e. can have partial strings, such as:
db
or
db:server
Trying to use:
(.*):?(.*)?:?(.*)?
selects the whole string
Please advise.
Give this one a shot:
([^:]*?):?([^:]*?):?([^:]*?)$
Not sure what language you're using, so it may not work.
Example: http://regex101.com/r/eQ6bF0
Note on the example it's set for a global/multiline match - beware that this will match across newlines if you don't use the correct modifier.
You didn't specify a language that I can see, so there may be different specific answers, but the basic problem is that .* will match a ":" character. That means the first term will suck the entire string in. I would use ([^:]*) instead of (.*).
You can try this:
([^:]+)(?::([^:]+)(?::([^:]+))?)?
I think this is what you're looking for:
(db|:server|:port)
will match any and all of these:
db:server:port
db
db:server
Working example:
http://regex101.com/r/rK1lI5

Related Links

Test but not select with regex
Pattern to delete lines causes the whole contents of the file to be deleted in Sed
Using a regex in a conditional vs. an outright awk/sed?
Regex for a valid month
Remove everything except a certain pattern
Hubot Not Responding to regex
RegExp to find first letter of words before #
Creating Index based on another field in logstash
Bug in R's grepl? [closed]
Regular Expression Google Form only numbers in a range
MS Word VBA macro to search and replace (Regex)
Repeat capture group ignoring whitespaces
regex help in notepad++ marking embedded values and copying into a new list
uWSGI routing rule is overriding static files
How to match more than one pattern and delete a line using sed?
move pattern in file over one column if match awk

Categories

HOME
validation
codeigniter
wix
identityserver4
performance-testing
tcl
blogs
ibm-midrange
webdriver-io
add-in
ceph
gcov
doorkeeper
higher-order-functions
rgb
api-design
aerospike
procedure
contains
jscodeshift
smartcard
software-distribution
co
netlify
centroid
php-carbon
abstract-syntax-tree
apache-httpcomponents
grouping
sql-server-2014-express
feedly
kprobe
rdf4j
debugview
skyscanner
vue-router
photo-gallery
stackexchange.exceptional
blockui
rhel7
api-doc
xaf
powerbuilder-conversion
distributed-lock
hydra
autofocus
rating-system
clientscript
xssf
ietf-netconf
python-module
flying-saucer
ocsp
data-fitting
balsamiq
insert-update
android-number-picker
occam-pi
oracle-bi
spl
session-replication
appv
heroku-toolbelt
voting
translucency
vbo
bonfire
structured-programming
alienvault
data-science-studio
sql-server-ce-3.5
alt
zim-database
android-tablelayout
brooklyn
zeroclipboard
mouseenter
azimuth
disk-io
pydatalog
pdf-scraping
sqlprofiler
pligg
datasift-python
qdebug
mms-gateway
hamlc
cloud-hosting
android-actionmode
ekeventkit
nsinvocation
bapi
junitperf
boost-gil
machine.config
keylogger

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