Reputation: 11
I know that there has already been some postings about the stack overflow error with regex and long strings, but they didn't help me and never concern my type of parsing problem.
I just try to find the string in parentheses from a mathematical function e.g.
funktionsstring
=SIN(3.141592653589793238462643383279502884197169399375105820974944592307816406
286208998628034825342117067982148086513282306647093844609550582231725359408
12848111745028410270193852110555964462294895493038196);
Using the following code with a pattern to find the string x which is in parentheses eg. ( x ) :
Pattern pattern = Pattern.compile("\\([^(]*?\\)");
Matcher matcher = pattern.matcher(funktionsstring);
I get the following error
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.regex.Pattern.bitsOrSingle(Pattern.java:2553)
at java.util.regex.Pattern.range(Pattern.java:2601)
at java.util.regex.Pattern.clazz(Pattern.java:2507)
at java.util.regex.Pattern.sequence(Pattern.java:2030)
at java.util.regex.Pattern.expr(Pattern.java:1964)
at java.util.regex.Pattern.compile(Pattern.java:1665)
at java.util.regex.Pattern.<init>(Pattern.java:1337)
at java.util.regex.Pattern.compile(Pattern.java:1022)
at classes.Parser.Klammerauswertung(Parser.java:104)
at classes.Parser.Klammerauswertung(Parser.java:119)
at classes.Parser.Klammerauswertung(Parser.java:119)
I don't see if I can improve the pattern in some way to prevent iteration which seems to cause the stack overflow. Obviously the split function doesn't work here.
Also - as the string is very long - I would like to allow n\ as character. I want to work generally with big decimal numbers (using apfloat) to obtain at least some 100 to 1000 exact decimals for scientific reasons, is it possible to keep regex changing the pattern?
If not, how do I have to rewrite regex?
Is there a better tool?
Upvotes: 1
Views: 448
Reputation: 2363
SIN\((\d+\.?\d*)\)
I edited to allow for numbers with decimals. Instead of random .
and digits.
(?:SIN|COS|TAN)\(([-]?\d+\.?\d*\^?\d*)\)
this allows for the functions sin, cos, or tan to be used to add more functions just add a |{functionname}
at the start. Also, it can be a negative value with an exponent.
or you can have
.+\(([-]?\d+\.?\d*\^?\d*)\)
This will allow for anything to be in front of the ()
I don't quite understand what you want with the * - /
what i suggested is that you make separate REGEX for each function so you can handle them differently.
Upvotes: 2