String Pattern Matching

Hi,

I am working on a project that needs to identify a particular String from another String, however, the location where the match will occur is unknown. For example, I would want to identify a line that includes operators such as "++" or "--".

if (Pattern.matches("++", lineSplit[j])){

....//other irrelevant codes here

}

wherelineSplit[j] is an element from an already set String array.

I'm considering usingPattern.matches for this, but it shows me a compiling error:

java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0

++

^

I've tried usingsplit too, but it shows a similar error.

The last resort would be to use other pattern matching algorithm to search the entire document, but it would be MUCH, MUCH less efficient to code. Is there any way to solve this? or do I have to brace myself for the new RAM chip I'll be needing for this?

[1137 byte] By [Heloisec] at [2007-9-30 19:54:44]
# 1
You can use String.indexOf("++") -> returns the index of the first character of the argument. if the argument does not occure in the string, -1 is returned!
alexh79 at 2007-7-7 0:41:46 > top of Java-index,Java Essentials,Java Programming...
# 2

The reason your application doesnt work is because Pattern & Matcher are part of Java regexp implementation, regexp is short for regular expressions, a very powerfull tool for text matching, however some characters have special meaning in patterns, '.','+','*','?','(',')','[',']' ... to name a few.

I think the pattern you may wanna match against would be ".*//+//+.*"

I know regexp is kinda hairy when you first look at it, but its a very powerfull tool once you learn how to use it.

'+' means previous char / group once or more times

'.' any char except cr/lf (unless you include cr/lf explicitly)

'*' previous char zero or more times

'/' escapre char (also java escape char unfortunately, hence the double '//'s

Pattern: "ab+" matches "ab","abb","abbb" etc.

jhhdk at 2007-7-7 0:41:46 > top of Java-index,Java Essentials,Java Programming...
# 3
> I think the pattern you may wanna match against would> be ".*//+//+.*"A minor, the escape character is \ and not /. > '/' escapre char (also java escape char unfortunately,> hence the double '//'sSee above./Kaj
kajbj at 2007-7-7 0:41:46 > top of Java-index,Java Essentials,Java Programming...
# 4

I have an extra question on this topic myself. I've tried to match a test string for (int i=0;i<10;i++) { with the pattern .*\\+\\+.* and it succeeded. That's all very nice, but it also succeeds when I have three + symbols instead of just two. I then tried to change the pattern into (.*\\+\\+.*){2}, but that didn't work the way I thought it would. Could someone show me how to do this the right way:

String testGood = "for (int i=0;i<10;i++) {";

String testBad = "for (int i=0;i<10;i+++) {";

boolean okGood = Pattern.matches(".*\\+\\+.*", testGood);

boolean okBad = Pattern.matches(".*\\+\\+.*", testGood); // Not good, don't want true on +++

hdevaan at 2007-7-7 0:41:46 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi,

Your expression fails because .* will match any character, so it will match +.

I don't know if this is the best way to do it, becuase it uses both lookahead and lookbehind.

String pattern = ".*(?<!\\+)[+]{2}(?!\\+).*";

String testGood = "for + (int i=0; i<10; i++) {";

String testBad = "for + (int i=0; i<10; i+++) {";

System.out.println(Pattern.matches(pattern, testGood));

System.out.println(Pattern.matches(pattern, testBad));

/Kaj

>

kajbj at 2007-7-7 0:41:46 > top of Java-index,Java Essentials,Java Programming...