1

I've got a large number of regular expressions to check, that are outputted in the Java/JavaScript RegEx format as shown:

\d{7}|\d{8}
[a-zA-Z]\d{8}
[a-zA-Z]\d{3}-\d{3}-\d{2}-\d{3}\d[0|1]

Seeing as I have a comfort with Bash, but there are little tricks I may never think of, I was wondering if there is a way I can take a test string to evaluate against this specific format of RegEx.

I've tried a number of greps and evaluating with data I know passes through a RegEx tester as such http://www.cyber-reality.com/regexy.html

In using bash RegEx I can evaluate these correctly, however I don't have the ability to change the way my server outputs these strings to a format I know how to evaluate in bash.

Any thoughts?

2
  • Tough to do with bash's regex. As a quick test, I'd go with the Gnu grep with the -P option (Perl regexp). However, that only works until you encounter something particular to Java's regexp. @Sir_Athos approach is the only way to handle everything that Java does. Commented Aug 29, 2013 at 18:05
  • Yeah certainly, my idea was a little far fetched and I was looking for a quick fix to my problem (which is always a bad idea). I'll go with his solution as it will avoid me writing an interpreter. Commented Aug 30, 2013 at 16:04

1 Answer 1

1

I'd recommend writing a command line tool that does the checking. You can then call this tool from a bash script.

Using this starting point, here's something that does what you need:

import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegexTestHarness {
    public static void main(String[] args) {
        try{
            Pattern pattern = Pattern.compile(args[1]);
            Matcher matcher = pattern.matcher(args[2]);

            boolean found = false;
            while (matcher.find()) {
                System.out.println(matcher.group() + ", position " + matcher.start() + ":" + matcher.end() + "\n");
                found = true;
            }
            if (!found) {
                System.out.println("\nNo match found.");
                System.exit(1);
            }
        }
        catch (Exception e) {
            System.out.println("\nCaught an exception. Description: " + e.getMessage());
            System.exit(2);
        }
    }
}

To check if there is any match, you can use this script:

#!/bin/bash
if java RegexTestHarness "my_regex" "my_input_string"; then
    echo "Expression matches"
else
    echo "Expression doesn't match"
fi

To check for a particular result, you can capture the output of the command and process it in your script.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'll go with a similar approach. I was trying a fairly farfetched idea and made it more simple in my mind than it really is. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.