简体   繁体   English

正则表达式VB.net我应该分组吗?

[英]Regex VB.net Should I Group?

I have wrote the following code to cut down the information I have in each line 我编写了以下代码,以减少每一行中的信息

s = Regex.Replace(s, "(a\/users\/\d*).*\(.*", "$1")

The string starts as this: 字符串如下所示:

/a/users/15/badges?params%5Bnotifications%5D%5Bcount%5D=5 HTTP/1.1" 200 143 "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" / a / users / 15 / badges?params%5Bnotifications%5D%5Bcount%5D = 5 HTTP / 1.1“ 200143” Mozilla / 5.0(兼容; MSIE 9.0; Windows NT 6.1; Trident / 5.0)

Im trying to get to 我试图去

/a/users/15/ (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" / a / users / 15 /(兼容; MSIE 9.0; Windows NT 6.1; Trident / 5.0)”

Am I going far wrong? 我做错了吗? If anyone can assist I would be grateful. 如果有人可以提供帮助,我将不胜感激。


When i try this: 当我尝试这个:

s = Regex.Replace(s, "(a/users/\\d*). (\\Blackberry. ).*", "$1 $2")' s = Regex.Replace(s,“(a / users / \\ d *)。 (\\ Blackberry。 )。*”,“ $ 1 $ 2”)'

To sort the following line 对以下行进行排序

/a/users/80021/messages.json?params[page]=1&params[per_page]=10&params[set_actions]=true HTTP/1.1" 200 13063 "BlackBerry9320/7.1.0.398 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/603 tibbr/3.6.6.1" 1/1574070 /a/users/80021/messages.json?params[page]=1&params[per_page]=10&params[set_actions]=true HTTP / 1.1“ 200 13063” BlackBerry9320 / 7.1.0.398配置文件/MIDP-2.1配置/CLDC-1.1 VendorID / 603 tibbr / 3.6.6.1“ 1/1574070

It does not work it doesnt seem to recognise the / after the /a/users/80021 它不起作用,它似乎无法识别/ a / users / 80021之后的/

Thanks again for your help, im learning so much its great. 再次感谢您的帮助,即时消息非常好。

i will have aa look at HttpUtility as well 我也会看一下HttpUtility

You shouldn't escape the slashes and match the third group as well: 您不应该逃脱斜线并匹配第三组:

(a/users/\d*/).\((.*)\).*

You also need to end the regex with .* to make sure the remaining characters are removed. 您还需要以.*结尾的正则表达式,以确保删除了其余字符。

Or: 要么:

s = Regex.Replace(s, "(a/users/\d*/).*(\(.*\)).*", "$1 $2")

DEMO . 演示

But as @Cory argues: one better uses library implemented algoritms like HttpUtility since these are designed following all specifications and less likely to contain bugs. 但是正如@Cory所说:最好使用库实现的算法,例如HttpUtility因为这些算法是按照所有规范设计的,并且不太可能包含错误。

EDIT : About your second regex, there is an error in: 编辑 :关于您的第二个正则表达式,有一个错误:

s = Regex.Replace(s, "(a\/users\/\d*).*(\Blackberry\.*).*","$1 $2")

You don't escape the brackets, but B and . 您不会逃脱括号,但是B. , the correct regex is probably: ,正确的正则表达式可能是:

s = Regex.Replace(s, "(a\/users\/\d*/).*(Blackberry.*).*","$1 $2")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM