简体   繁体   English

VB.Net正则表达式随机字符串

[英]VB.Net regex random string

I have regex code that gets string between 2 strings from TextBox1. 我有正则表达式代码,从TextBox1获取2个字符串之间的字符串。

TextBox1 looks something like this: TextBox1看起来像这样:

href="www.example.com/account/05798/john123">
href="www.example.com/account/4970/max16">
href="www.example.com/account/96577/killer007">
href="www.example.com/account/3077/hackerboy1337">
href="www.example.com/account/43210/king42">

So, it will get value from href="www.example.com/account/4321/ to "> (usernames) 因此,它将从href="www.example.com/account/4321/"> (用户名)获得价值


The problem is, how to do it? 问题是,该怎么办? My regex code: 我的正则表达式代码:

(?<="href=""www.example.com/account/RANDOM_STRING/")(.*?)(?="">)

I know i could replace RANDOM_STRING with \\w{4} , but some IDs are 5-digit. 我知道我可以用\\w{4}替换RANDOM_STRING ,但有些ID是5位数。

Or another option would be to do this 或者另一种选择是这样做

 Dim strOne As String = "www.example.com/account/43210/king42"
 Dim strMain As String = Split(strOne, "/account/")(1)
 Dim strSubOne As String = Split(strMain, "/")(0)
 Dim strSubTwo As String = Split(strMain, "/")(1)

You need a negated character class [^/] that matches any char but a / . 你需要一个否定的字符类[^/]来匹配任何字符而不是/ So, replace RANDOM_STRING with [^/]* . 所以,用[^/]*替换RANDOM_STRING

Also, in a regex pattern, to match . 此外,在正则表达式模式中,匹配. , you need to escape the dot - \\. ,你需要逃避点 - \\. .

Thus, your regex pattern can be fixed as 因此,您的正则表达式可以固定为

(?<="href=""www\.example\.com/account/[^/]*/").*?(?="">)

However, you may user a simpler regex with a capturing group: 但是,您可以使用捕获组来使用更简单的正则表达式:

"href=""www\.example\.com/account/[^/]*/"(.*?)"">

The value you need is in Match.Groups(1).Value . 您需要的值是Match.Groups(1).Value

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

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