Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Question on regular expression.

Thread view: 
Paul - 15 Mar 2007 16:22 GMT
Here is a pattern and the code snippet I have:

String pat = "#foo#([+-][0-9]+)*";
String input = "#foo#+1-2";
Pattern pattern = Pattern.compile(pat);
Matcher matcher = pattern.matcher(input);

while (matcher.find())
{
  String matched = matcher.group(1);
  ...
}

The result I'm hoping for is to get "+1" and "-2" returned for
"matched" string. However, I'm only getting "-2" and then matcher is
done.

Can any shed some light on this?

Thanks,
Paul.
Patricia Shanahan - 15 Mar 2007 16:49 GMT
> Here is a pattern and the code snippet I have:
>
[quoted text clipped - 17 lines]
> Thanks,
> Paul.

There is only one "#foo#" to match the start of the expression, so only
the first find() call can succeed.

What does matcher.groupCount() return after the successful find()? Just
looking at it, I would have expected 2, with group(1) equal to "+1" and
group(2) equal to "-2", but you only show a call for group(1).

Patricia
Lars Enderin - 15 Mar 2007 16:55 GMT
Paul skrev:
> Here is a pattern and the code snippet I have:
>
[quoted text clipped - 14 lines]
>
> Can any shed some light on this?

Since #foo# is part of pat, you cannot get more than one match. Remove
#foo# from pat and also remove the *.
Paul - 15 Mar 2007 17:23 GMT
> Paul skrev:
>
[quoted text clipped - 21 lines]
>
> - Show quoted text -

The input can be "#foo#", "#foo#+1", "#foo#+1-2", etc. The current
approach I took is to modify the input and reset the matcher each time
I got a match. I don't like this approach. It's kind of like what you
indicated here. Basically, you have to use 2 patterns during the
process. Right?

Patricia,

It only returns 1 group. I was hoping it behaves like what you
described.

Thanks,
Paul.
Steve Wampler - 15 Mar 2007 18:02 GMT
> Here is a pattern and the code snippet I have:
>
[quoted text clipped - 14 lines]
>
> Can any shed some light on this?

I believe that only one group is defined in the above pattern, and since
'*' is greedy, that group gets assigned the last successful match of
the contained subpattern.  The following varient shows this better:
---------------------------------------------------
import java.util.regex.*;

public class Foo {

   public static void main(String[] args) {
       String pat = "#foo#([+-][0-9]+)([+-][0-9]+)*";
       String input = "#foo#+1-2+3-4";
       Pattern pattern = Pattern.compile(pat);
       Matcher matcher = pattern.matcher(input);

       if (matcher.matches()) {
           System.out.println("Found "+matcher.groupCount()+" groups");
           for (int i = 1; i <= matcher.groupCount(); ++i) {
               System.out.println(""+i+": "+matcher.group(i));
               }
           }

       System.exit(0);
       }

   }
--------------------------------------------------
When run, the output is:
--------------------------------------------------
->java Foo
Found 2 groups
1: +1
2: -4
->
--------------------------------------------------

Unfortunately, I can't see how to specify an arbitrary number of groups...

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.

Patricia Shanahan - 15 Mar 2007 18:35 GMT
>> Here is a pattern and the code snippet I have:
>>
[quoted text clipped - 14 lines]
>>
>> Can any shed some light on this?

...
> Unfortunately, I can't see how to specify an arbitrary number of groups...

I think it is going to have to be done with two patterns.
As I understand the situation, the current pattern does a good job of
representing the substring that should be matched, so how about keeping
something like it, but adding another pattern for splitting up the
repeated groups?

  Pattern outer = Pattern.compile("#foo#(([+-][0-9]+)*)");
  Pattern inner = Pattern.compile("[+-][0-9]+");

  void test(String input) {
    Matcher outerMatcher = outer.matcher(input);
    if (outerMatcher.find()) {
      System.out.printf("Matched %s in %s%n",outerMatcher.group(0),input);
      Matcher innerMatcher = inner.matcher(outerMatcher.group(1));
      while(innerMatcher.find()){
        System.out.printf("Group %s%n",innerMatcher.group(0));
      }
    } else {
      System.out.println("No match: " + input);
    }
  }
Steve Wampler - 15 Mar 2007 19:08 GMT
>> Unfortunately, I can't see how to specify an arbitrary number of
>> groups...
>
> I think it is going to have to be done with two patterns.

I agree.  I *should* have said "...arbitrary number of groups
in a single pattern".

Signature

Steve Wampler -- swampler@noao.edu
The gods that smiled on your birth are now laughing out loud.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.