Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Regex replacing entire text rather then match

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Regex replacing entire text rather then match

    Hello,

    With the code below, I am trying to replace all regex matches for visa cards within a given text file.

    My first test was with a text "new3.txt" exclusively containing the visa test card 4111111111111111. My objective was to replace the card with "xxxx-xxxx-xxxx-xxxx". This was successful.

    However, when modifying the text file to include other characters and text before and after (ex: " qwerty 4111111111111111 adsf zxcv"), it gives mixed results. Although it successfully validates the match, it replaces the whole text in the file, rather than replacing solely the match.

    When trying this search and replace with words (rather than a regex match), it does not have this behavior. What am I missing?

    import java.io.*;
    import java.util.regex.*;
    public class BTest
        {
    	//VISA Test
         private final static String PATTERN = "(?s).*\\b4[0-9]{12}(?:[0-9]{3})?\\b.*";	
         public static void main(String args[])
             {
             try
                 {
                 File file = new File("c:/eclipse/new3.txt");
                 BufferedReader reader = new BufferedReader(new FileReader(file));
                 String line = "", oldtext = "";
                 while((line = reader.readLine()) != null)
                     {
                     oldtext += line + "\r\n";
                     }
                 reader.close();
                 String newtext = oldtext.replaceAll(PATTERN, "xxxx-xxxx-xxxx-xxxx");
     
                 FileWriter writer = new FileWriter("c:/eclipse/new4.txt");
                 writer.write(newtext);writer.close();
             }
             catch (IOException ioe)
                 {
                 ioe.printStackTrace();
             }
         }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Regex replacing entire text rather then match

    Welcome to the forum! Thank you for taking the time to learn to post code correctly.

  3. #3
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Regex replacing entire text rather then match

    Your regex seems a bit complicated for it's required task, I suggest looking it over.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Regex replacing entire text rather then match

    Quote Originally Posted by KucerakJM View Post
    Your regex seems a bit complicated for it's required task, I suggest looking it over.
    I am following a regex which validates VISA cards, as specified by an article on regular-expression dot info called "Finding or Verifying Credit Card Numbers" (cant seem to post the link).

    edit:..... and you were right... on each end, I only truly needed the "\\b"... Now it behaves correctly. Thanks!

  5. #5
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Regex replacing entire text rather then match

    It's worth mentioning that the article you mentioned assumed that all non-numeric, or at least spaces and dashes, were removed from the string. This meant that the regex they provided wasn't exactly what you were looking for, but very close.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Regex replacing entire text rather then match

    Very good point.

    That is precisely an issue I'm trying to fix... My current "lazy" approach is to remove all special characters between 4 numbers... It's causing slight issues with my data, because I'm modifying more than the desired matches... But the impact is minor, so it's o.k. for the moment...

    And also I guess this topic is for another thread...

    Thanks!

Similar Threads

  1. Replacing text after a certain character or symbol in a text file
    By Ravekitty in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 25th, 2013, 02:40 PM
  2. [SOLVED] JAVA Text File Processing with REGEX
    By tonionio in forum Java Theory & Questions
    Replies: 9
    Last Post: February 28th, 2013, 05:01 AM
  3. [SOLVED] Replacing words in a text file
    By sanelko in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: January 30th, 2012, 01:56 AM
  4. Regex code to match character * in IpAddress
    By jeevana in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 30th, 2011, 05:46 AM
  5. Reading in entire contents of text file to one string
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: December 12th, 2010, 07:03 PM