Results 1 to 5 of 5
Thread: Regex pattern
- 12-10-2007, 06:34 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
Regex pattern
I am using Regex API to filter some text. I want to find the following pattern:
*-*.html
A dash has to appear in the file name.
I don't know how to include dash (-) in the pattern string.Java Code:String patternStr = "[A-Za-z0-9]*.html"; Pattern pattern = Pattern.compile(patternStr); String str1; while ((str1 = in.readLine()) != null) { Matcher matcher = pattern.matcher(str1); while(matcher.find()) { int start = matcher.start(); int end = matcher.end(); System.out.println(str1.subSequence(start, end)); }
- 12-10-2007, 08:26 PM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Escape it with a double backslash (since you cant have a single backslash):
Java Code:String patternStr = "*\\-*.html";
- 12-10-2007, 10:05 PM #3
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
Thanks but what does * means here. Does it mean all the possible characters? I came up with the following. Does this makes sense?
Java Code:String patternStr = "[A-Za-z0-9]*\\-[A-Za-z0-9]*.html";
- 12-11-2007, 09:58 AM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Sorry i missed your reply :\
This should work just fine:
What that says is:Java Code:Pattern p = Pattern.compile("^(\\w+[-]\\w+).html");
^ means the beggning of the line. it gets rid of problems like asd$#!%sd-tasd.html
\\w A word character: [a-zA-Z_0-9]
+ one or more character
() mean to take the entire input
Sorry about my previous reply, i wasnt quite awake :p.
small note: you dont need the \\ for -. You can have them, it wont hurt anything, but they are not required since - is not a special character.
- 12-11-2007, 10:20 AM #5
Senior Member
- Join Date
- Nov 2007
- Posts
- 115
- Rep Power
- 0
Similar Threads
-
Regex for file extension
By gapper in forum New To JavaReplies: 1Last Post: 01-31-2008, 03:59 PM -
Using Scanner with regex.MatchResult
By Java Tip in forum Java TipReplies: 0Last Post: 01-18-2008, 02:08 PM -
Regex Quantifiers Example
By Java Tip in forum Java TipReplies: 0Last Post: 01-10-2008, 10:44 AM -
Handling regular expressions using Regex
By Java Tutorial in forum Java TutorialReplies: 0Last Post: 01-07-2008, 12:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks