Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 使用正则表达式获取参数_Regex_Url - Fatal编程技术网

Regex 使用正则表达式获取参数

Regex 使用正则表达式获取参数,regex,url,Regex,Url,嗨,就在我想我懂雷格士的时候,我被一条湿漉漉的鱼打了个耳光。有人能帮我吗?我快到了,但就是想不出最后一部分 使用以下URL进行测试,以及使用正则表达式尝试匹配的内容: URL === Regex needs to match --------------------------------------------------------------------- http://localhost#stq=textare

嗨,就在我想我懂雷格士的时候,我被一条湿漉漉的鱼打了个耳光。有人能帮我吗?我快到了,但就是想不出最后一部分

使用以下URL进行测试,以及使用正则表达式尝试匹配的内容:

URL                                        === Regex needs to match
---------------------------------------------------------------------
http://localhost#stq=textarea&c=all        === http://localhost
http://localhost?#stq=textarea&c=all       === http://localhost?
http://localhost/#stq=textarea&c=all       === http://localhost/
http://localhost/?#stq=textarea&c=all      === http://localhost/?

http://localhostq=textarea&c=all           === http://localhost
http://localhost?q=textarea&c=all          === http://localhost?
http://localhost/q=textarea&c=all          === http://localhost/
http://localhost/?q=textarea&c=all         === http://localhost/?

http://localhost/test#stq=textarea&c=all   === http://localhost/test
http://localhost/test?#stq=textarea&c=all  === http://localhost/test?
http://localhost/test/#stq=textarea&c=all  === http://localhost/test/
http://localhost/test/?#stq=textarea&c=all === http://localhost/test/?

http://localhost/testq=textarea&c=all      === http://localhost/test
http://localhost/test?q=textarea&c=all     === http://localhost/test?
http://localhost/test/q=textarea&c=all     === http://localhost/test/
http://localhost/test/?q=textarea&c=all    === http://localhost/test/?
可以更改#stq参数,因此它不会总是以#开头。此外,域并不总是本地主机,可能有也可能没有路径名

到目前为止,我的正则表达式到目前为止是什么,但如果有路径名,它不会被接受

^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i
我在这里创建了一个示例:

任何帮助都会很棒

(?您可以使用

(?<=http:\/\/localhost[\/|\?]).*
^https?:\/\/(?:(?!q=)[^\n?=#])+\??
部分地

  • ^
    字符串的开头
  • https?:\/\/
    将协议与可选的
  • (?:(?!q=)[^\n?=#]+
    匹配除
    \n?=#
    中的一个字符之外的任何字符,并且后面不跟
    q=
  • \??
    匹配可选的


如果
#
q=
始终存在,则可以使用非贪婪匹配,然后断言
#
q=

^https?:\/\/.*?(?=#|q=)

你想把域名和其余参数分开吗?你能给出每个例子所需的输出吗?如果我能做到,那就更好了。目前我正在使用正则表达式来识别主机和路径名(如果预设)然后删除它,这样我就只剩下参数了,然后我循环通过这些参数映射搜索字段。因此,对于示例URL,这就是我试图从每个URL中匹配的内容(一旦我删除了域名/路径名,就应该给我留下参数):为什么不使用
var uri=new System.uri(“http://localhost/test/#stq=textarea&c=all")
然后访问
uri.Fragment
?我不能使用uri.Fragment,因为参数上的#可能不在字符串中,该参数可以输入为q=…甚至query=…这是捕捉OP说他们不想捕捉的路径。非常感谢,这正是我要寻找的!!!