I have a very long string, somewhere in this string, there is an url. In this example this url is at the beginning.
"http://localhost:1234/api/$metadata#this_entry_is_variable_and_can_exist_of_numbers_and_characters/$entity","Version":"AAAEEEIIU=""
I'm trying to write a RegEx in C# for this particular string, to extract the url after the following rules:
/api
, but it can be any characters /api
, it is always /$metadata
/$metadata
there is a hashtag #
followed by a string of any characters /$entity
This is the RegEx I have come up with so far:
(^http://\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\^$metadata$(\#(\[a-zA-Z0-9)(\$(\entity$))]*).*?)
When testing this in LinqPad, the following issues occur:
Can anyone give me a hint on were to continue, as I'm stuck..
Your regex doesn't follow a Regular Expression constructing rules hence no expected match. This is what you are expressing:
https?://[^/]+/[^/]+/\$metadata#[^/]+/\$entity
Try this regex:
https?://[\w-]+(?:\.[\w-]+)*(?::\d+)?/.*?\$metadata#.*?\$entity\b
To you questions:
You matched only one regex because of the ^
. It matches only the start of input string if RegexOptions.Multiline
is not set, and also start of every new line (after newline chars) if RegexOptions.Multiline
is set.
The regex gets mixed up in the part where $metadata...entity$
is surrounded by []
See 2.
Simply make the s
optional with ?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.