java


Regular expression to parse option string


I'm using the Java matcher to try and match the following:
#tag TYPE_WITH_POSSIBLE_SUBTYPE -PARNAME1=PARVALUE1 -PARNAME2=PARVALUE2: MESSAGE
The TYPE_WITH_POSSIBLE_SUBTYPE consists of letters with periods.
Every parameter has to consist of letters, and every value has to consist of numerics/letters. There can be 0 or more parameters.
Immediately after the last parameter value comes the semicolon, a space, and the remainder is considered message.
Everything needs to be grouped.
My current regexp (as a Java literal) is:
(#tag)[\\s]+?([\\w\\.]*?)[\\s]*?(-.*=.*)*?[\\s]*?[:](.*)
However, I keep getting all the parameters as one group. How do I get each as a separate group, if it is even possible?
I don't work that much with regexps, so I always mess something up.
If you want to capture each parameter separately, you have to have a capture group for each one. Of course, you can't do that because you don't know how many parameters there will be. I recommend a different approach:
Pattern p = Pattern.compile("#tag\\s+([^:]++):\\s*(.*)");
Matcher m = p.matcher(s);
if (m.find())
{
String[] parts = m.group(1).split("\\s+");
for (String part : parts)
{
System.out.println(part);
}
}
System.out.printf("message: %s%n", m.group(2));
The first element in the array is your TYPE name and the rest (if there are any more) are the parameters.
Try this out (you may need to add extra '\' to make it work within a string.
(#tag)\s*(\w*)\s*(-[\w\d]*=[\w\d]*\s*)*:(.*)
By the way, I highly recommend this site to help you build regular expressions: RegexPal. Or even better is RegexBuddy; its well worth the $40 if you plan on doing a lot of regular expressions in the future.

Related Links

is it possible to use an interface to speak to another class in java?
How do I line up my JLabel with the row of JLabels below it
changing ActionProvider to appcompat-v7
Best way to pass a POJO to a spring MVC controller
For Loop Error Message
Cron Job success on time but not executing Ofy() query
How to get all values from database id to jcombobox?
How to make an exe or a setup to distribute my Java(+DB) project made in NetBeans
Can I get additional information about the error from SAXParseException and XMLStreamReader
Spring MVC ModelAttribute as Interface
BaseAdapter Fragment Change page
Best method for copying file from MTP device to PC
Query opitimization in MongoDB using Java
Relationship between two objects
Android studio, remembering input when closing app
How to compare String Array and count similar words

Categories

HOME
testing
xamarin
openlayers
date
blogger
amazon-ec2
magnific-popup
c#-4.0
nullpointerexception
appx
bpmn
read-eval-print-loop
android-4.4-kitkat
razor
filtering
sqlite3
v8
gps
sql-server-2016
windows-10-universal
phaser
ibm-odm
carthage
worldwind
nhibernate-envers
custom-wordpress-pages
kryo
physics-engine
tokenize
css-animations
lcd
sqlcipher
shopware
internet-explorer-9
fifo
ping
bpel
nouislider
skia
javascriptcore
xquery-3.0
exuberant-ctags
airconsole
rotational-matrices
mpmediaquery
mapdb
hexo
bower-install
revolution-slider
simplexml
git-merge
abstract-class
streamsets
streamreader
azure-ml
core-plot
heightmap
convertapi
websphere-mq-fte
btrace
user-accounts
dotcover
rails-routing
veracode
drupal-6
nodebb
np-complete
nbconvert
blacklist
migradoc
ableton-live
url-pattern
angular-cache
vmware-tools
file-writing
gridview-sorting
actionbardrawertoggle
yaws
revolution-r
two-factor-authentication
freelancer.com-api
prerequisites
ora-00900
tarjans-algorithm
sframe
cakephp-3.1
mono-embedding
generic-programming
remobjects
streambase
system.reflection
angularjs-ng-click
id3v2
markers
xojo
jboss-weld
quartz-graphics
ril
ora-00911
cos
ember-app-kit
pygit2
factory-method
free-variable
propertyeditor
tridion-worldserver
dbproviderfactories
bigcouch
boost-filesystem
invite
table-footer
ecl
sproutcore-2
nhibernate.search
meego
libs
nerddinner
spec#
ntvdm.exe

Resources

Encrypt Message