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?