java


How do I create regular expression for a string to match [#username:4]?


I need to create a regular expression to detect a string matching the format below for Java and Php:
[#username:4]
where 'username' can be any text and '4' can be any integer. I tried creating one myself but I am new to the world of regular expressions and was unable to do so. This is the furthest I got :
([[]+[#]+[A-Za-z0-9])\w+
Any help will be great.Thanks!
A quick way of doing it would be to use this regular expression:
\[#\w+:\d+\]
Test Here
which does what you asked for but has a disadvantage. It doesn't take the length of the username or the integer into account.
I don't know if it is important in your case but i personally would prefer this regex:
\[#\w{1,25}:\d{1,5}\]
Test Here
Which does exactly the same as the one above but doesn't allow for infinitely long username or number and sets the bounds for both.
In perticular, \w{1,25} means that it will match any word character (a-zA-Z0-9_) between 1 and 25 times (inclusive). Therefore the longest username is 25 characters long and the shortest possible is 1 character long. These values can be tweaked.
Likewise it restricts the integer length between 1 and 5 characters therefore any integer with more than 5 digits or less than 1 is invalid: \d{1,5} Again, the values can be tweaked.
Would the username ever have :'s in it? If not use the following
\[#([^:]+):(\d+)\]
https://regex101.com/r/7iqrPm/1
If the username would never have brackets then use the following:
\[#([^:\]]+)(?::(\w+))?\]
It also makes the :integer part optional
https://regex101.com/r/FmfAze/3
\[#\w+\:\d+\]
\w means all word can be username, it supports a-z,A-Z,0-9 and _
\d means all digital, 0-9

Related Links

Generic error: “Error uploading to the iTunes Store” in submission
Calculator and using arrays
How do I get an aggregated list of external dependencies of my maven project?
System.out.printf method throws a flags exception
How to get an URL location from a server path for a property file
find intersection between multiple sets
Apache solr decide solr search results priorty
SnmpV3AdaptorServer snmpV1Trap throws IOException and not releasing its resource
Java visualvm missing tabs and disabled buttons
Error in compilation of code with lambda expression
File output keeps overwriting?
How to monitor if my App switches from running in foreground to running in background and vice versa?
Can object reference from thread local variable be shared to other threads safelly?
How to get Aspect to fire for annotated functions?
gcj error when trying to compile hadoop mapreduce examples
Running SWT GUI from jar file is different (and faulty) but works perfectly running from eclipse

Categories

HOME
c#-4.0
dictionary
json.net
office365api
sqlite-net-extensions
c#-2.0
disassembler
mouse
floating-action-button
circuit
ef-migrations
footer
flux
immutable.js
opencover
errorlevel
realex-payments-api
crosstab
vlsi
saas
dbclient
twilio-api
uisplitview
ioio
jspresso
rotational-matrices
html5-fullscreen
host
ios5
devextreme
git-merge
neuroscience
uft-api
unobtrusive-validation
android-fingerprint-api
opshub
azure-ml
graphenedb
ws-security
ensembles
pdf-reactor
nand2tetris
android-mediaprojection
netcdf4
rainbowtable
azure-application-gateway
pango
return-value
veracode
statsd
setter
flutterwave
spring-cache
color-profile
vtigercrm
gnome-shell-extensions
firmata
asp.net-4.5
autorest
pundit
microbenchmark
php-ci
umbraco6
javax.mail
facebook-graph-api-v2.4
xc16
camanjs
wordpress-theme-customize
markers
article
doskey
mysql-error-1062
concurrent-collections
uv-mapping
floating-point-precision
c18
hungarian-algorithm
pygit2
jelly
factory-method
e4x
rdoc
coderush
frameset
manchester-syntax
wse3.0
disclosure
site.master
dmx512
javap
lzh

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