1

I understand that when I do [^abc] this will match any thing other than a,b, and c. What if I want it to match anything other than a "..". So far the exclusion list I have is:

[^<>:\"/\|?*]+

I want to add a ".." as well into this exclusion list. So in english it would be "if it's anything other than the left brackets, right brackets, double quote, asterix, double dot (".."), the rest of the characters here, then it should match".

The test case I need to pass is:

foo/../baz needs to be /baz
bar/../../foo needs to be /../foo
xonegirlz
  • 8,889
  • 19
  • 69
  • 127
  • possible duplicate of [Regular expression to match string not containing a word?](http://stackoverflow.com/questions/406230/regular-expression-to-match-string-not-containing-a-word) –  Nov 09 '11 at 22:53
  • I saw that , but I am confused on how to incorporate that into my existing regex – xonegirlz Nov 09 '11 at 22:55
  • Your `double dot ("..")` is not a character, it is *two* characters - a string - and you can't put a string in a *character class* whether or not it's negated. – Stephen P Nov 09 '11 at 23:19
  • @Stephen so what is the way to do this then? – xonegirlz Nov 09 '11 at 23:34
  • @xonegirlz - The Way depends on what you're trying to do. Your "test case I need to pass" is some help, but the statement "needs to be" is vague. It would be helpful if you could state it like "I'm writing a function X that, given a String like Y, would return a String like Z. I'm trying to use a regex-replace to find ______ in Y and replace all execpt _________ to return Z". You're current question is asking only about negated character classes, and the answer to that is "You can't do ("..") that way" (Turned this comment into an answer) – Stephen P Nov 10 '11 at 17:40

5 Answers5

0

Not a java expert, but it looks like you have a negated character class defined there. A character class is basically a list of characters in that class, or in your case, not in that class, and you can apply this to a string.

It seems that you're most likely after a match for the string "..". If so, I think you just need a specific regex for it. Maybe this would do the trick:

\.\.

A dot "." by itself of course matches any single character, so the backslash escapes are needed to match an actual string of two dots instead of any two characters.

bigendian
  • 788
  • 4
  • 11
0

If java can do lookahead assertions, one way is this:

(?:(?!\.\.)[^<>:"/|?*])+ untested

edit the above will match up until the first ..
Its not clear what you are trying to do, but to validate the entire string to these conditions, simply add ^$ -
^(?:(?!\.\.)[^<>:"/|?*])+$ untested

0

Ok I have been playing around for a bit and this will prevent a match if the .. string is present:

^(?:(?!(\.\.)).)*$

I'm going to carry on but you might consider simply running two separate regex and making sure neither match.

rtpHarry
  • 13,019
  • 4
  • 43
  • 64
0

If you are not particularly interested in delimiter itself you could use this

    String source = "aa^bb<cc>dd:ee\"ff/gg\\hh|ii?jj*kk:ll/mm..mn";
    String regex = "[<>:\"/\\\\|?*^]+|[.]{2}";
    String[] splits = source.split(regex);
    System.out.println(Arrays.toString(splits));

Output

[aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, mn]
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
0

@xonegirlz - The Way to exclude .. depends on what you're trying to do. Your "test case I need to pass" is some help, but the statement "needs to be" is vague.

It would be helpful if you could state it like

"I'm writing a function X that, given a String like Y,
would return a String like Z.
I'm trying to use a regex-replace to find ______ in Y
and replace all execpt _______ to return Z".

You're current question is asking only about negated character classes, and the answer to that is You can't do ("..") that way

Stephen P
  • 14,422
  • 2
  • 43
  • 67