Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm not very good at RegEx, can someone give me a regex (to use in Java) that will select all whitespace that isn't between two quotes? I am trying to remove all such whitespace from a string, so any solution to do so will work.

For example:

(this is a test "sentence for the regex")

should become

(thisisatest"sentence for the regex")

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
286 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here's a single regex-replace that works:

s+(?=([^"]*"[^"]*")*[^"]*$)

which will replace:

(this is a test "sentence for the regex" foo bar)

with:

(thisisatest"sentence for the regex"foobar)

Note that if the quotes can be escaped, the even more verbose regex will do the trick:

s+(?=((\["]|[^"])*"(\["]|[^"])*")*(\["]|[^"])*$)

which replaces the input:

(this is a test "sentence "for the regex" foo bar)

with:

(thisisatest"sentence "for the regex"foobar)

(note that it also works with escaped backspaces: (thisisatest"sentence "for the regex"foobar))

Needless to say (?), this really shouldn't be used to perform such a task: it makes ones eyes bleed, and it performs its task in quadratic time, while a simple linear solution exists.

EDIT

A quick demo:

String text = "(this is a test "sentence "for the regex" foo bar)";
String regex = "\s+(?=((\\["]|[^"])*"(\\["]|[^"])*")*(\\["]|[^"])*$)";
System.out.println(text.replaceAll(regex, ""));

// output: (thisisatest"sentence "for the regex"foobar)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...