Results 1 to 5 of 5

Thread: Regular Expressions on the Contents of a File

  1. #1
    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.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    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

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    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) 
    { 
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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‎

  5. #5
    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();
        }
      }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,843,768.04340 seconds with 16 queries