If I have header name value pair such as:
accept-encoding: gzip, deflate, br
:authority: stackoverflow.com
something: some:value
How do I only split after the header name? Meaning, following would be my results:
accept-encoding => gzip, deflate, br
:authority => stackoverflow.com
something => some:value
I have tried str.Split(":", 2)
but it doesn't handle the :authority header
CodePudding user response:
You can split with :
that is preceded with a word char and not followed with a word char:
str.split("\\b:\\B")
See the regex demo. Alternatively, you may split with :
that is preceded with a non-whitespace char and is followed with a whitespace:
str.split("(?<=\\S):(?=\\s)")
See this regex demo.
Details:
\b:\B
- a word boundary (\b
) requires a letter, digit or_
to appear immediately to the left of the:
(as:
is a non-word char) and the next non-word boundary\B
requires a non-word char (any char other than letter, digit or underscore) to appear immediately on the right, or end of string can occur there, too(?<=\S)
- a positive lookbehind that matches a location immediately preceded with a non-whitespace char(?=\s)
- a positive lookahead that matches a location immediately followed with a whitespace char.
CodePudding user response:
If you only want to split on the first occurrence of :
and allow an optional :
at the start of the string, you could also make use of a lookbehind with a finite quantifier in Java {0,n}
where n in this example is 100
(?<=^:?[^:\r\n]{0,100}):
In Java
str.Split("(?<=^:?[^:\\r\\n]{0,100}):")
(?<=
Postive lookbehind^:?[^:]{0,100}
From the start of the string, match an optional:
and 0-n occurrences of any char except:
or a newline
)
Close the lookbehind:
Match the colon to split on