Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
1  
I guess you meant [^)]*, not [^(]* also the ? there doesn't make much sense. –  Kent Sep 11 '13 at 19:52

1 Answer 1

 SIN\((\d+\.?\d*)\)

Regular expression visualization

Edit live on Debuggex

I edited to allow for numbers with decimals. Instead of random . and digits.

(?:SIN|COS|TAN)\(([-]?\d+\.?\d*\^?\d*)\)

Regular expression visualization

Edit live on Debuggex

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*)\)

Regular expression visualization

Edit live on Debuggex

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.

share|improve this answer
    
Hello, I modified a pattern found in internet which worked for small numbers. The given expression is just an example off course: any mathematical formula needs to be possible and may contain +-*/ but also ^ for exponents, decimal numbers and any letter for functions (exp, sin, cos). Is this still possible? –  Thommy7571 Sep 13 '13 at 10:01
    
@Thommy7571 i updated my answer. –  progrenhard Sep 13 '13 at 16:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.