in java regex


i new in java. getting java stack overflow exception in regex strhinditext. should that?

try {      // regex convert pattern "{\fldrslt {\fcs1 \ab\af24 \fcs0 &#2345;}{"      // "{\fldrslt {\fcs1 \ab\af24 \fcs0 &#2345;}}}{"      // strhinditext = strhinditext.replaceall("\\{(\\\\fldrslt[ ])\\{((\\\\\\s+[ ])+)((\\s*&#\\d+;\\s*(-|,|/|\\(|\\)|\"|;|\\.|'|<|>|:|\\?)*)+)\\}\\{","{$1{$2$4}}}{");       // regex convert pattern "{\fcs0 \af0 &#2345;{ or {\fcs0 \af0 *\tab &#2345;{"       // "{\fcs0 \af0 &#2345; }{"      strhinditext = strhinditext.replaceall("\\{\\s*((\\\\\\s+[ ](\\*)?)+\\s*)(-|,|/|\\(|\\)|\"|;|\\.|'|<|>|:|\\?)*[ ]*(((&#\\d+;)[ ]*(-|,|/|\\(|\\)|\"|;|\\.|'|<|>|:|\\?)*[ ]*)+)\\{", "{$1 $4$5 }{");       // regex convert pattern "{&#2345; \fcs0 \af0 {"       // "{&#2345; \fcs0 \af0 }{"      strhinditext = strhinditext.replaceall("\\{\\s*(((&#\\d+;)[ ]*(-|,|/|\\(|\\)|\"|;|\\.|'|<|>|:|\\?)*[ ]*)+)[ ]*((\\\\\\s+[ ])+)\\{", "{$1 $5 }{");       } catch(stackoverflowerror er) {             system.out.println("third try block stackoverflowerror in regex pattern reform rtf tags................");             er.printstacktrace();         //  throw er;      } 



whenever these strhinditext contain large data gives java stackoverflow exception:

java.lang.stackoverflowerror 2013-08-08 15:35:07,743 error [stderr] (http-127.0.0.1-80-9)    @ java.util.regex.pattern$curly.match0(pattern.java:3754) 2013-08-08 15:35:07,743 error [stderr] (http-127.0.0.1-80-9)    @ java.util.regex.pattern$curly.match(pattern.java:3744) 2013-08-08 15:35:07,744 error [stderr] (http-127.0.0.1-80-9)    @ java.util.regex.pattern$grouptail.match(pattern.java:4227) 2013-08-08 15:35:07,744 error [stderr] (http-127.0.0.1-80-9)    @ java.util.regex.pattern$bmpcharproperty.match(pattern.java:3366) 2013-08-08 15:35:07,745 error [stderr] (http-127.0.0.1-80-9)    @ java.util.regex.pattern$curly.match0(pattern.java:3782) 2013-08-08 15:35:07,745 error [stderr] (http-127.0.0.1-80-9)    @ java.util.regex.pattern$curly.match(pattern.java:3744) 



strhinditext data is:

 `{\rtlch\fcs1 \af1\afs18 \ltrch\fcs0 \f1\fs18\cf21\insrsid13505584 &#2349;&#2379;&#2346;&#2366;&#2354;&#32; &#2404; \par }\pard\plain \ltrpar\s16\ql \li0\ri0\sb100\sa100\sbauto1\saauto1\sl240\slmult0\widctlpar\wrapdefault\aspalpha\aspnum\faauto\adjustright\rin0\lin0\itap0\pararsid13505584 \cbpat20 \rtlch\fcs1 \af0\afs24\alang1025 \ltrch\fcs0 \fs24\lang1033\langfe1033\cgrid\langnp1033\langfenp1033 {\rtlch\fcs1 \ab\af1\afs18 \ltrch\fcs0 \cs21\b\f1\fs18\cf21\insrsid13505584 &#2309;&#2344;&#2381;&#2357;&#2375;&#2359;&#2339;&#32;&#2325;&#2352;&#2375;&#2306;&#32; :}{\rtlch\fcs1 \af1\afs18 \ltrch\fcs0 \f1\fs18\cf21\insrsid13505584  \par &#2349;&#2379;&#2346;&#2366;&#2354;&#32;&#44;&#32;&#2350;&#2343;&#2381;&#2351;&#32;&#2346;&#2381;&#2352;&#2342;&#2375;&#2358;&#32;&#2325;&#2368;&#32;&#2352;&#2366;&#2332;&#2343;&#2366;&#2344;&#2368;&#32;&#2346;&#2381;&#2352;&#2366;&#2325;&#2371;&#2340;&#2367;&#2325;&#32;&#2360;&#2369;&#2306;&#2342` 

option 1 - treat symptoms

look recursive calls in regex.

if not sure problem lies: try a regex tester this.

option 2 - treat cause (much better)

don't use regex if there better tools task.

in case could: search rtf parsing library or write own parser.
e.g. 1 here jahroy pointed out in comments.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -