0

I'm currently using this regex pattern = "(?ius)[(?<=\\s)]\\bgo\\b(?=\\s)".

The purpose if to filter t-sql commands with the "go" as a delimiter.

Example t-sql code:

select * from table1 go
select * from table2
go select * from table3

The pattern works with the split() method, but it does not work with the find() method.

Example result with split() method:

select * from table1
select * from table2
select * from table3

Example result with find() method:

select * from table1 go
select * from table2
go select * from table3

It seams to have a problem when the "go" is at the end of the line or at the beginning. If the first "go" had a space after it, it works. I've already tried several things without any luck. In my case I really need to use the find() method, I can't just rely on split, because I need confirmation that the a "line" as a delimiter before doing the split.

4
  • I assumed you meant matches() instead of matcher(), but now I'm not sure that was right. Can you show the actual code you're using? Commented Nov 25, 2013 at 16:08
  • I meant matcher(). See answer bellow and comment for example. Commented Nov 25, 2013 at 20:02
  • Okay, then I'll just remove that entirely. The matcher() method just creates a Matcher object so you can call its find() method, so you don't need to mention it. Commented Nov 25, 2013 at 22:56
  • find() returns a boolean value to tell you whether there was any string or substring that matcher your regex. So your find() on that list should return true. So can you please explain what you are trying to do... Commented Nov 25, 2013 at 23:06

3 Answers 3

1

I can’t reproduce your problem:

String pattern="(?ius)[(?<=\\s)]\\bgo\\b(?=\\s)";
String s="select * from table1 go\n" + 
        "select * from table2\n" + 
        "go select * from table3";
for(String sub: s.split(pattern))
{
  System.out.println("sub: "+sub);
}
System.out.println();

Pattern p=Pattern.compile(pattern);
Matcher m=p.matcher(s);
int pos;
for(pos=0; m.find(); pos=m.end())
  System.out.println("sub: "+s.substring(pos, m.start()));
System.out.println("sub: "+s.substring(pos));

Will produce the same result for both ways. It would be very surprising to find a difference here, as the String.split method does the same thing internally.

Sign up to request clarification or add additional context in comments.

2 Comments

In my code I deal with each line separately. There seems to be a problem with the end/beginning of the line. Try to run your code again but this time with the String: String s = "select * from table1 go\n" + "go select * from table2 go";
Your pattern says that there must be a space after go so if your string ends without any character after go the pattern does not match. The same applies when you double the word go and have only a single spacing character (line-feed) between them as it will be removed by the first match and therefore the second can’t find a space before the word. But there’s still no difference between find and split.
0

To better clarify the problem above:

The problem doesn't seem to be with split() being different from matcher(), the problem is with the regex and the beginning and ending of the string.

My code treats each line separately so this was what fool me.

The regex "(?ius)[(?<=\s)]\bgo\b(?=\s)" is not catching the beginning "go" and ending "go".

The idea is catching the "whole word", in this case the word is "go" so if the word is at the beginning of the string or at the end, i still want to include it.

See example and comment above for clarification.

Still need to fix this problem, not having much success...

Comments

0

I think i have found the solution

(?iu)[(?=\\s)]\\b(Go)|(^go)|(go$)\\b(?!\\S)

This pattern seams to negate the special characters without negating the start and end of line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.