Take this example:
string1 = "aaxadfxasdf"
string2 = "asdfxadf"
string3 = "sadfas"
i need a pattern that matches x such that it returns a match for x only for string2.
Note: for the purpose of this question, i must use java regex. Also, i don’t have access to the code, so i can’t simply check the # of matches returned by the java regex object.
Hey All,
Thanks for the responses, some of which do answer my question. My example wasn’t as specific as i needed it to be. So let me add this quirk to the mix:
what if i’m matching more than a single character?
string4 = "This is a sentence!"
string5 = "This is just another sentence with some repetition in it. This is a sentence"
string6 = "this is"
In this case, i want to match “sentence” only if it appears in the string only once.
Also, though not represented in the example, i need to be able to search across lines.
I’m not sure if you are only expecting letters, or if symbols and numbers might be included before and after the
xyou want to match. If so, use[^x]instead of[a-wy-z], like so:If you are also wanting to match
xby itself, replace the+symbols with*like so: