[英]Regex to pick out url part of a long string
I have a very long string, somewhere in this string, there is an url. 我有一个很长的字符串,在该字符串的某个地方,有一个URL。 In this example this url is at the beginning.
在此示例中,该URL在开头。
"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: 我正在尝试使用C#为此特定字符串编写一个RegEx,以遵循以下规则提取URL:
/api
, but it can be any characters /api
,但可以是任何字符 /api
, it is always /$metadata
/api
,始终为/$metadata
/$metadata
there is a hashtag #
followed by a string of any characters /$metadata
之后,有一个#
后跟任何字符的字符串 /$entity
/$entity
结尾 This is the RegEx I have come up with so far: 到目前为止,这是我想出的RegEx:
(^http://\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\^$metadata$(\#(\[a-zA-Z0-9)(\$(\entity$))]*).*?)
When testing this in LinqPad, the following issues occur: 在LinqPad中进行测试时,会发生以下问题:
Can anyone give me a hint on were to continue, as I'm stuck.. 任何人都可以给我一个提示,因为我被困住了。
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. 如果未设置
RegexOptions.Multiline
则仅与输入字符串的开头匹配;如果设置了RegexOptions.Multiline
则仅与每个新行的开头(在换行符之后) RegexOptions.Multiline
。
The regex gets mixed up in the part where $metadata...entity$
is surrounded by []
正则表达式在
$metadata...entity$
被[]
包围的部分中混杂在一起
See 2. 见2。
Simply make the s
optional with ?
只需将
s
可选?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.