nlipatov
nlipatov

Reputation: 53

RegExp matching certain characters after a word

I'm having problems with a regular expression. I have a string like this:

host=HOME_SERVER value=\"83.169.217.62 - km-meat-170929 [04/May/2018:00:01:13 +0300] \\\"POST /platform/services/2.0/EnterpriseService HTTP/1.1\\\" 200 4075 \\\"-\\\" \\\"1C+Enterprise/8.3\\\" 0.141\" 1526823558639987327

I would like to find all backslashes and spaces after "value", so far I've come up with this:

value(.+)

How to find further backslashes and spaces?

The proposed solution works fine in java:

 s.replaceAll("(\\G(?!^)|value=)([^\\\\\\s]*)[\\s\\\\]+", "$1$2")

How can I change it to not delete matches, but replace them from, for example, "*"?

Upvotes: 0

Views: 54

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You want to remove all whitespace and backslashes after the value= substring.

So, the pattern to remove is [\s\\], but the trick is to make it only match after a certain value. This is the case where the \G operator can help. You may match the value= or the end of the previous match with (\\G(?!^)|value=) and then get to the whitespace or backslash using ([^\\\\\\s]*). Note the capturing groups, we may later restore these captures using $1$2 replacement backreferences in the replacement pattern.

You may use

s.replaceAll("(\\G(?!^)|value=)([^\\\\\\s]*)[\\s\\\\]+", "$1$2")

See the regex demo

Details

  • (\\G(?!^)|value=) - Group 1 (its value is referred to with $1): end of the previous match (\\G(?!^)) or (|) a value= substring
  • ([^\\\\\\s]*) - Group 2 (its value is referred to with $1): zero or more chars other than whitespace (\s) or \\ chars
  • [\\s\\\\]+ - one or more whitespace (\s) or \\ chars

Upvotes: 2

Related Questions