java


String pattern matching with regular expression in java [duplicate]


This question already has an answer here:
Java regular expressions and dollar sign
4 answers
I'm trying pattern matching expression for a below string. But it doesn't work. could you anybody help me on this ? Only Alphanumeric and underscore allowed inside,Both side
$ sign will be there. Ex strings: Test_1,23_test_2,test3.
String text = "$test_1$";
Pattern p = Pattern.compile("$([A-Za-z0-9_])$");
Matcher m = p.matcher(text);
m.matches();
if (m.find()) {
System.out.println("Matched: " + m.group(1));
} else {
System.out.println("No match.");
}
$ is a regex meta character and should be escaped, try this
Pattern p = Pattern.compile("\\$([A-Za-z0-9_]+)\\$");
Your regex should be:
Pattern p = Pattern.compile("(\\$[A-Za-z0-9_]*\\$)");
You could simply do...
s.matches("\\$[a-zA-Z0-9_]*\\$")

Related Links

Custom Hibernate validator not working in a DropWizard application
JSON assign Java String
Selenium behavior changes in Windows 7 and Windows Server 2016
httpcontext in Java RESTful service
Weblogic Spring application gives error for css and js
How to save and load image move in android?
Deploy multiple Java apps on a server
Java JDBC types and code snippets
As String class is the subclass of Object class how the return type of toString() method in the Object class is String
Killing a java.lang.Process not killing subprocess()
Interaction with locked ViewHolder in RecyclerView using ItemTouchHelper
Oracle error when executing stored procedure: ORA-06502: PL/SQL: numeric or value error
libgdx - Simple repeating texture (setWrap())
Designing an “updatable” static set for use with Spring?
LCD 1602 and PI4J raspberry
Does android build-tools 25 support jdk 1.8?

Categories

HOME
maven
pandas
date
blogger
proxy
reflection
server
mockito
stock
spring-jdbc
ip
retrofit
alpha
echarts
add
iggrid
php-7.1
windows-azure-storage
alignment
n-gram
designer
libtiff
angular-ui
undefined
ab-testing
swingx
errorlevel
dcevm
captiveportal
crystal-reports-2010
facet
fifo
elasticsearch-ruby
libssl
google-cloud-nl
gsmcomm
lightswitch-2013
preg-match
frame
junit5
create-table
grails-3.1
uiswipegesturerecognizer
mpmediaquery
ios5
appfabric
dartium
space-complexity
webix-treetable
glew
mime
heightmap
btrace
executenonquery
cubic-spline
xml-documentation
jlink
return-value
statsd
python-cryptography
sqldf
quartz-composer
cubes
time-and-attendance
deadbolt-2
color-picker
httplistener
trash
player
flash-cs5
prettytensor
cartesian-product
thredds
citrus-pay
angular-leaflet-directive
ionic
ibaction
appfabric-cache
fluid-dynamics
oxwall
phalanger
fpml
wordpress-theme-customize
googlemock
tld
viadeo
industrial
ms-project-server-2010
soundtouch
jplaton
rabl
django-nonrel
jquery-mobile-dialog
sqlperformance
comaddin
type-equivalence
flashvars
ocunit
referrer
angularjs-controller
gil
broken-links
word-processor
tridion-worldserver
ticoredatasync
django-tagging
xmemcached
table-footer
telerik-scheduler
site.master
microsoft-virtualization
genealogy

Resources

Encrypt Message