-
How to create REGEX in Java
hi ,evertbody
I am very new to Java. I plan to search a string in my HTML file in Regular Expression.In my file, it exists the string I want to find. But it does not give me the right result. maybe the pattern is not right.
Who can tell me how to create a Pattern to find the string "Welcome".
Thanks a lot!
The following is my code:
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class TitleMatch
{
FileReader fis;
String username;
TitleMatch(FileReader fis,String username)
{
this.fis=fis;
this.username=username;
}
public void findString()
{
// open reader for file
try
{
//InputStreamReader in = new InputStreamReader(fis);
// read contents into string buffer
String input ;
String patternString = ".*Welcome.*";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE+Pattern.UNICODE_CASE);
BufferedReader in = new BufferedReader(fis);
while ((input=in.readLine())!=null)
{
if (input.equals("")) return;
Matcher matcher = pattern.matcher(input);
System.out.println(matcher.matches());
if (matcher.matches())
{
int start = matcher.start();
int end = matcher.end();
String match = input.substring(start, end);
input = matcher.replaceAll(match + " "+ username);
//int leng=input.length();
//input=input.subString(1,end)+ username+input.subString(1,end)+
//input.subString((end++),leng);
}
System.out.println(input);
}
}
catch (IOException e)
{
e.printStackTrace();
}
catch (PatternSyntaxException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
FileReader fis=new FileReader("welcome.html");
TitleMatch tm =new TitleMatch(fis,"judy");
tm.findString();
}
catch(FileNotFoundException e)
{
}
}
}
Similar Threads
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10-04-2002, 05:32 PM
-
By Dharmesh in forum .NET
Replies: 4
Last Post: 10-01-2001, 03:47 PM
-
By Keith Franklin, MCSD in forum java.announcements
Replies: 0
Last Post: 08-18-2000, 06:37 PM
-
By H. Wilson in forum Java
Replies: 6
Last Post: 04-14-2000, 11:25 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks