Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 19-02-2010
Member
 
Join Date: Nov 2009
Posts: 678
Regular Expressions on the Contents of a File

Hello, I am learning java programming and while learning it, I am not able to understand the Regular Expressions on the Contents of a File. I have use different sources for it, but all of them are useless. So, if you are having any solution to achieve it, then please help me to achieve it. If you know how to apply Regular Expressions on the Contents of a File then provide source code for that.
Reply With Quote
  #2  
Old 19-02-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: Regular Expressions on the Contents of a File

Hello, if you want to apply the regular expressions on the content of the file then you must need to make use of the following methods and classes:
Methods:
  • FileInputStream(filename).getChannel()
  • Charset.newDecoder()
  • Charset.decode()
Classes:
  • FileChannel
  • CharBuffer
  • Charset
Reply With Quote
  #3  
Old 19-02-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Regular Expressions on the Contents of a File

Hello, for Applying Regular Expressions on the Contents of a File you need to use the code below. Just create a class and then add all of the code in that and get your problem solved.
Code:
try 
{ 
Pattern P = Pattern.compile("P");
Matcher M = P.M(fromFile("FileName.txt")); 
while (M.find()) 
{ 
String str = M.group(); 
} 
} 
catch (IOException e) 
{ 
}
Reply With Quote
  #4  
Old 19-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
Re: Regular Expressions on the Contents of a File

For getting better solution on this you can simply make use of the books below and solve your problem:
  • The Java developers almanac 1.4: examples and quick reference
  • Automating InDesign with Regular Expressions‎
  • Java regular expressions: taming the java.util.regex engine‎
  • Thinking in Java‎
  • Mastering regular expressions‎
Reply With Quote
  #5  
Old 19-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Regular Expressions on the Contents of a File

For getting the regular expressions on the content of a file you must need to use the code provided below:
Code:
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main 
{
  public static CharSequence fromFile(String filename) throws IOException 
{
    FileInputStream fileinputstream = new FileInputStream(filename);
    FileChannel filechannel = fileinputstream.getChannel();
    ByteBuffer bytebuffer = filechannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) filechannel.size());
    CharBuffer charbuffer = Charset.forName("8859_1").newDecoder().decode(bytebuffer);
    return charbuffer;
  }
  public static void main(String[] argv) throws Exception 
{
    Pattern patt = Pattern.compile("patt");
    Matcher match = patt.match(fromFile("infile.txt"));
    while (match.find()) 
{
      String string = match.group();
    }
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Regular Expressions on the Contents of a File"
Thread Thread Starter Forum Replies Last Post
Question on regular expressions Logan 2 Software Development 5 12-02-2010 04:21 AM
Regular Expressions in Java Ash maker Software Development 5 03-02-2010 04:42 AM
Regular expressions for unicode Solaris Software Development 4 15-12-2009 12:20 PM
file maker to display the contents of last record Bigga Lexx Windows Software 3 09-09-2009 03:26 PM
Display Contents of XML File fastrod Software Development 3 14-03-2009 12:52 PM


All times are GMT +5.5. The time now is 10:23 PM.