简体   繁体   中英

VB.Net regex random string

I have regex code that gets string between 2 strings from TextBox1.

TextBox1 looks something like this:

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)


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.

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 [^/]* .

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 .

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.

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