Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java regex - eval for replacement

807601Jun 18 2008 — edited Jun 21 2008
Hi

Could you please help me in a regex problem?

I have a regex pattern in a string. I want to apply this on a text, and wherever it is applied the matched text should be replaced by the length of the found text. Now I have done it with a while loop, but it takes too long to process:
			Pattern p = Pattern.compile(patternString);
			Matcher m = p.matcher(text);

			// I dont know how to get this job done with m.replaceAll(). Thats why the following loop
			// By the way, in Perl there is "eval" with which we can do this
			
			while (m.find()) {
				String foundText = m.group();
				Pattern p2 =  Pattern.compile( Pattern.quote(foundText) );
				text = p2.matcher(text).replaceAll( foundText.length() );
			}
Is there a better way? Kindly help.

Thanks
Sri
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 19 2008
Added on Jun 18 2008
3 comments
314 views