Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-10-2007, 08:34 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
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.

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)); }
I don't know how to include dash (-) in the pattern string.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-10-2007, 10:26 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Escape it with a double backslash (since you cant have a single backslash):
Code:
String patternStr = "*\\-*.html";
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-11-2007, 12:05 AM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks but what does * means here. Does it mean all the possible characters? I came up with the following. Does this makes sense?

Code:
String patternStr = "[A-Za-z0-9]*\\-[A-Za-z0-9]*.html";
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-11-2007, 11:58 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Sorry i missed your reply :\
This should work just fine:
Code:
Pattern p = Pattern.compile("^(\\w+[-]\\w+).html");
What that says is:
^ 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 .

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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-11-2007, 12:20 PM
Senior Member
 
Join Date: Nov 2007
Posts: 115
ravian is on a distinguished road
Thanks for the details.
I appreciate this.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex for file extension gapper New To Java 1 01-31-2008 05:59 PM
Using Scanner with regex.MatchResult Java Tip Java Tips 0 01-18-2008 04:08 PM
Regex Quantifiers Example Java Tip Java Tips 0 01-10-2008 12:44 PM
Handling regular expressions using Regex Java Tutorial Java Tutorials 0 01-07-2008 02:46 PM
Matching Patters using Regex JavaForums Java Blogs 0 11-01-2007 06:29 PM


All times are GMT +3. The time now is 04:40 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org