• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help on Regex  RSS feed

 
Tushar Ravi
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I am not able to get the desired results. Can any one of you help me?



The results are
1----------(SUM(AVG(M1)))

What i expect is
1----------(SUM(AVG(M1)))
2----------(M1)

How can i achieve this?

Thanks in advance.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you seem to realize the ".*" in your regex is greedy and swallows everything after the first "AVG" including the second AVG so there is nothing to find() after the first match! If I understand the problem you are trying to solve it seem to me that you need to create a new matcher targeted with the group of the previous match each time you find "AVG". Seems a weird requirement but ....
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There also seems to be some misunderstanding of groups (and the group count). There is only one group in the regular expression -- and that doesn't change based on the string being match against.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Ravi wrote:How can i achieve this?

Answer: With difficulty. And possibly not at all with a single regex (although you might be able to engineer a tortuous solution).

regex is a pattern-matching program, and for that it's superb; but what you have is a syntax issue - specifically, the fact that (presumbly) any function can take an expression that involves another function.

My advice: write an expression parser. Now you might well be able to do that with a recursive program that uses Matchers, or MatchResults, and groups.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!