How to split a regular expression into parts?

Yuval

I need to break down a regular expression into its basic parts. For instance, given the regex [a-d]+[r-z]* I need to split it into [a-d]+ and [r-z]*. This is of course a very simple example, and regex syntax can get very complex...

Is there a (relatively) simple way to achieve this, or am I doomed to reverse-engineer a regex parser?

I need this to find out if a given string is a part of matching input for a given regular expression.

Danny Daglas

You can brute-force it this way:

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class RegexSplitter {    
    private static boolean tryRegex(String regex) {
        try {
            Pattern.compile(regex);
            return true;
        } catch(PatternSyntaxException pse) {
            return false;
        }
    }

    public static void main(String args[]) {
        String input = "[a-d]+[r-z]*";

        List<String> results = new ArrayList<>();

        int start = 0;
        int end = 1;
        boolean good = false;
        while(end < input.length()) {
            String part = input.substring(start, end);
            if(!tryRegex(part)) {
                if(good) {
                    good = false;
                    results.add(input.substring(start, end - 1));
                    start = end-1;
                }
            } else {
                good = true;
            }
            ++end;
        }
        if(tryRegex(input))
            results.add(input.substring(start,end));

        System.out.println(results);
    }
}

// Output: [[a-d]+, [r-z]*]

It's hacky and heuristic, but it may work for your purposes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to split a regular expression into parts?

From Dev

How to return parts of a string with regular expression

From Dev

How to Split those regular expression

From Dev

how to split a string in clojure not in regular expression mode

From Dev

How to Split string using a regular expression?

From Dev

How to construct a regular expression to split this text?

From Dev

How to split 2 strings using regular expression?

From Dev

How to split specific line using regular expression

From Dev

Extracting parts of a string with regular expression

From Dev

Split with regular expression

From Dev

javascript split regular expression

From Dev

Regular Expression/String split

From Dev

Regular Expression Split and match

From Dev

javascript split regular expression

From Dev

Regular Expression/String split

From Dev

Regular Expression Split

From Dev

Regular expression split string

From Dev

How to split string with defining multiple regular expression in clojure?

From Dev

How to split String in Scala but keep the part matching the regular expression?

From Dev

How to split a stream to stdout+stderr based on regular expression pattern?

From Dev

How can I split the xml file using regular expression in Perl

From Dev

PHP: How to use Regular Expression in PHP to Split String in 2

From Dev

How to split this string and get the number separately in ios regular expression

From Dev

How to I write regular expression that split by include token and exclude token

From Dev

How do I split a file each time a regular expression occurs?

From Dev

Regular Expression in Google Analytics for two parts of string

From Dev

Capturing parts of string using regular expression in R

From Dev

regular expression to extract different parts of a url

From Dev

Regular Expression in Google Analytics for two parts of string

Related Related

  1. 1

    How to split a regular expression into parts?

  2. 2

    How to return parts of a string with regular expression

  3. 3

    How to Split those regular expression

  4. 4

    how to split a string in clojure not in regular expression mode

  5. 5

    How to Split string using a regular expression?

  6. 6

    How to construct a regular expression to split this text?

  7. 7

    How to split 2 strings using regular expression?

  8. 8

    How to split specific line using regular expression

  9. 9

    Extracting parts of a string with regular expression

  10. 10

    Split with regular expression

  11. 11

    javascript split regular expression

  12. 12

    Regular Expression/String split

  13. 13

    Regular Expression Split and match

  14. 14

    javascript split regular expression

  15. 15

    Regular Expression/String split

  16. 16

    Regular Expression Split

  17. 17

    Regular expression split string

  18. 18

    How to split string with defining multiple regular expression in clojure?

  19. 19

    How to split String in Scala but keep the part matching the regular expression?

  20. 20

    How to split a stream to stdout+stderr based on regular expression pattern?

  21. 21

    How can I split the xml file using regular expression in Perl

  22. 22

    PHP: How to use Regular Expression in PHP to Split String in 2

  23. 23

    How to split this string and get the number separately in ios regular expression

  24. 24

    How to I write regular expression that split by include token and exclude token

  25. 25

    How do I split a file each time a regular expression occurs?

  26. 26

    Regular Expression in Google Analytics for two parts of string

  27. 27

    Capturing parts of string using regular expression in R

  28. 28

    regular expression to extract different parts of a url

  29. 29

    Regular Expression in Google Analytics for two parts of string

HotTag

Archive