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

Hibernate does not refresh entity childs completely
Android: How to make EditText digital input string into int?
How to set priorities for multiple threads in java [duplicate]
SimpleDateFormat gives API Error
NoSuchBeanDefinitionException in DelegatingFilterProxy
NullPointerException when using String from SQL database
Parsing Multiple Lines As Object Before Creating Tuple For Apache PIG UDF
How to return deep nested projections in Spring data rest?
How can I make this Variable by UserInput work? android
Material SearchBar Not Appearing
Implementing onClick in the Recyclerview Adapter
How do you extract numbers from an input file and use them for any output you want?
JavaFx Spreadsheet Cell Right Click Open Dialog
How to find com.dji.GSDemo.GoogleMap in the project navigator and create DJIDemoApplication Class?
Externalize web.xml params in a Spring application
Using SOAP 1.2 binding instead of SOAP 1.1

Categories

HOME
pandas
arduino-uno
log4j
urbancode
minimum-spanning-tree
drivers
kalman-filter
google-docs
rdf
v8
retrofit
installshield
datastax-java-driver
django-imagekit
applepay
slick-slider
ssl-client-authentication
spring-xd
wheelnav.js
handsontable
google-cloud-ml
jplayer
event-handling
lombok
pc
jqwidget
openedx
emulator
intentfilter
autosys
centos6.5
internet-explorer-9
entitlements
jaxb2
gsmcomm
skia
sql-server-2012-express
preg-match
iframe-resizer
android-kernel
picasso
host
restlet
streamsets
awt
particles.js
reportingservices-2005
firebase-admin
btrace
auto-update
dotcover
strptime
upstart
promela
ionicons
jlink
elgg
directory-structure
dwscript
django-scheduler
synchronous
medium.com
android-textview
ctest
prettytensor
ubuntu-10.04
yaws
goose
thredds
retina
libressl
responsive-images
sailfish-os
directoryservices
bitcoinj
android-radiobutton
heisenbug
mysql-error-1062
dexterity
runtime.exec
ftps
pygit2
distutils
angularjs-controller
dmoz
hamiltonian-cycle
armcc
pendrive
libc++
icanhaz.js
gin
web-architecture
nintendo-ds
mtj
project-hosting
w3wp
wsdl.exe
ntvdm.exe

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