How can I express in a regular expression "match up to a pattern without including this pattern"?

I have this input:

{http://sdfgdf.dfg.dfg.dfg#value1 : http://sdfgdf.dfg.dfg.dfg#value2}

and I would like to combine only the value1 with the first URL. I thought below regexp:

[^#]\w*\s:

but this corresponds to "value1:"

How can I express "match up to \ s :, not including this pattern?

+3
source share
2 answers

Sample of zero width. In Java, you can do it like this:

[^#]\w*(?=\s:)

You might consider making this regex more flexible, to handle extra spaces, etc. just in case the entrance is a little different from what you expect.

+3
source

, , Regex. , "" .

myInput.Split(" ")[0]; // Example in C#

...

myInput.Substring(0, myInput.IndexOf(" "));

...

(?=.*\s)[^\s]+

: " - , , ".

+1

Source: https://habr.com/ru/post/1793016/


All Articles