Results 1 to 4 of 4
- 04-07-2009, 04:33 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Regex - matching literal characters
Im trying to match the following pattern using regex:
The string begins with a literal '\' is followed by any number of letters and/or numbers and ends with '&0]'
e.g. '\07761739009B&0]'
Im trying to devise my pattern but Im not exactly sure how to work with matching literal characters, I was lead to believe a '//' would dictate that the character is literal but this doesnt work:
Thanks in advance for any suggestionsJava Code:Pattern Serial = Pattern.compile("(\\/.*+\\&0\\])");
-
What about a pattern String like so:
e.g.,Java Code:String patternString = "^\\\\.*\\&0\\]$";
Java Code:public class RegexTrial { public static void main(String[] args) { String test = "\\07761739009B&0]"; String patternString = "^\\\\.*\\&0\\]$"; Pattern pattern = Pattern.compile(patternString); Matcher matcher = pattern.matcher(test); boolean matches = matcher.matches(); System.out.println(test); System.out.println(matches); } }
- 04-07-2009, 08:01 PM #3
In a regex, you use a \ to indicate a literal, so \\ would match '\'.
However, to use \ in a Java string, you need to escape it with another \, so double all the \ giving "\\\\" which will match '\'.
-
Hey Racha, please don't cross-post the same question on multiple fora without notifying us that you're doing this and without providing links. It's not fair to us to ask us to possibly duplicate work that has been solved elsewhere as we're all volunteers with lives and families of our own. If you do this again, you will risk losing many potential helpers from all fora. Seriously.
Similar Threads
-
regular expressions and string matching
By DennyLoi in forum New To JavaReplies: 1Last Post: 11-16-2007, 10:15 AM -
Help with signature matching
By cachi in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks