Results 1 to 3 of 3
  1. #1
    GenkiSudo is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default Regular expressions from command prompt

    I'm writing a program which takes four command-line arguments:
    (1) Input file
    (2) Output file
    (3) Pattern to match
    (4) Text to replace with
    The goal of the program is to take the input file and replace all patterns (a regular expression specified by argument 3) with a text (argument 4) and then write the results to another file (argument 2).

    I've written the program and it works just fine, but I've had some problems with argument specifying the text to replace with.

    How would I specify that I want to delete the pattern if found?

    My solution was that for deleting the pattern you would just run the program without the fourth argument.
    e.g. If I want to delete all "gg" or "cc" strings from the file:
    java TextReplacer input.txt output.txt [gc]{2} (I just ommit the fourth argument)

    Part of the code that deals with command line arguments:
    Java Code:
    public class TextReplacer {
    
    	public static void main(String[] args) {
    		
    		String inputFilename = args[0];
    		File inFile = new File (inputFilename);
    		
    		String outputFilename = args[1];
    		File outFile = new File (outputFilename);
    		
    		String searchPattern = args[2];
    		
    		String replacementString;
    		
    		//if no replacement string is provided, program just deletes the search pattern
    		if (args.length < 4) replacementString = "";
    		else replacementString = args[3];
    Is there a more elegant way of doing this, something like actually entering the replacement string that means 'no character' or something of that kind?

    I'm using the replaceAll method from the Matcher class to do the actual string replacement

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default

    A comment; Test that there are at least 3 args before trying to use them.

  3. #3
    GenkiSudo is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    A comment; Test that there are at least 3 args before trying to use them.
    Thanks, I completely forgot about it.

Similar Threads

  1. Using Quantifiers in regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 10:43 AM
  2. Handling regular expressions using Regex
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 01-07-2008, 12:46 PM
  3. Capturing Groups using regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:19 AM
  4. Regular expressions quantifiers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 11:18 AM
  5. regular expressions and string matching
    By DennyLoi in forum New To Java
    Replies: 1
    Last Post: 11-16-2007, 10:15 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
  •