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

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

java - Regular expression to select all whitespace that isn't in quotes?

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

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

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...