在VB.NET中,从不同字符串的部分创建字符串
此字符串由我无法访问或更改的应用程序自动生成: “” 我需要将字符串更改为 “” 其中:在VB.NET中,从不同字符串的部分创建字符串,vb.net,string,Vb.net,String,此字符串由我无法访问或更改的应用程序自动生成: “” 我需要将字符串更改为 “” 其中: 来自上一个字符串 322来自issue=322 5来自第一个字符串中的page=5 但它也可能是不同的版本或页码,这是事先不知道的 如何在VB.NET中执行此操作?(必须是VB.NET)我已经尝试过使用拆分和比较的方法,但在解析字符串方面我是个灾难。欢迎帮助 编辑: 在尝试了下面Konrad的解决方案之后,当我尝试运行字符串时出现了一个错误。所有其他URL都可以正常工作,但一旦我将一个URL设置为需要转
- 来自上一个字符串
- 322来自issue=322
- 5来自第一个字符串中的page=5
在尝试了下面Konrad的解决方案之后,当我尝试运行字符串时出现了一个错误。所有其他URL都可以正常工作,但一旦我将一个URL设置为需要转换的格式,它就会出错 我怀疑这是因为转换函数是另一个函数的一部分,而我在尝试将regex函数放入时出错了。 这是完整的功能:
Function ExpandLine(ByRef sLine, ByVal nStart)
'Purpose: adapt expandLine into a funciton that replaces
' ' the urls form the UNIT with redirects
' '
' ' Purpose: This function searches recursively
' ' for strings embedded in "{" and "}" pairs.
' ' These strings contain a left and right part
' ' separated by ";". The left part will be
' ' hyperlinked with the right part.
' '
' ' Input: sLine - string to be expanded
' ' nStart - where to start the expansion from
' ' the right (normally set to -1)
' '
' ' Output: sLine - expanded string
' '
' ' Example: This line contains a {hyperlink;http://www.site.com}
' ' that points to the homepage
Dim n, n1, n2 As Integer
Dim sUrl As String
If nStart <> 0 Then
n = InStrRev(sLine, "{", nStart)
If n <> 0 Then
n1 = InStr(n, sLine, ";")
n2 = InStr(n, sLine, "}")
If Not (n1 = 0 Or n2 = 0) Then
sUrl = Mid(sLine, n1 + 1, n2 - n1 - 1)
'use RegEx to determine if its an UNIT url
Const TestPattern = _
"^http://[^/]+/locale=[^&]+&mag=[^&]+&issue=[^&]+&page=[^&]+&template=[^&]+$"
Dim conformsToPattern = Regex.IsMatch(sUrl, TestPattern)
If conformsToPattern Then
Const SitePattern = "(http://[^/]+)/"
Const IssuePattern = "issue=(\d+)"
Const PagePattern = "page=(\d+)"
Dim sSite = Regex.Match(sUrl, SitePattern).Groups(1).Value
Dim sIssue = Regex.Match(sUrl, IssuePattern).Groups(1).Value
Dim sPage = Regex.Match(sUrl, PagePattern).Groups(1).Value
sUrl = String.Format("{1}/{2}_{3}", sSite, sIssue, sPage)
End If
sLine = _
Left(sLine, n - 1) & "<a class=""smalllink"" target=""_new"" href=""" & _
sUrl & """>" & Mid(sLine, n + 1, n1 - n - 1) & "</a>" & _
Right(sLine, Len(sLine) - n2)
ExpandLine(sLine, n - 1)
End If
End If
End If
End Function
你想要:
这将分别搜索网站域的名称(包括前导的http://
,并由后一个正斜杠分隔)、issue
参数后的数字以及页面
参数后的数字
然后根据这三个发现构造结果字符串
通过\d+
搜索正则表达式中的数字,其中\d
匹配任何数字,+
告诉引擎至少匹配一个数字,并且可以任意匹配多个数字
对于网站,我们允许任何字符,除了正斜杠([^/]
–这是一个字符组,前导的^
告诉引擎对该组求反,即匹配不在其中的所有字符)
编辑:如果您首先想测试输入是否符合您的模式,您可以执行以下操作。但是,请注意,这个测试对GET参数的顺序很敏感,我认为这是一个警告信号,可以采取不同的做法:既然URL中GET参数的顺序并不重要,您能保证它保持不变吗
Const TestPattern = "^http://[^/]+/locale=[^&]+&mag=[^&]+&issue=[^&]+&page=[^&]+&template=[^&]+$"
Dim conformsToPattern = Regex.IsMatch(input, TestPattern)
If conformsToPattern Then
' Yes, go ahead. '
Else
' Nope, leave it unchanged. '
End If
这只是检查整个字符串(从start=
^
到end=$
)是否与模式匹配。变量参数值均编码为[^&]+
,即几个字符≠ <代码>&(这是参数的分隔符)。@ZombieSheep有效–是。但不幸的是,不是很优雅。我试着把它全部放在一个表达式中,但这会非常低效,并且对字符串中GET参数的顺序非常敏感!我一定会学到更多。我认为它会起作用,但我发现还有一些字符串(URL)不需要替换。因此,我需要先测试我的字符串是否是根据“”构建的,然后再转换成较短的版本。我不想意外地破坏我所有的链接。正则表达式也可以这样做吗?格式始终与中的格式相同:区域设置、杂志、问题、页面、模板(按此顺序),但它们的值可能会有所不同。@Konrad:非常感谢您的帮助。我马上就去试试。因为原始字符串是从应用程序生成的(它从来不是一个活跃使用的url,只是一篇自动生成的文章),所以我可以保证它的构建顺序/模式:)Regex的良好使用。如果您对正则表达式了解不多,也可以使用:String.SubString和String.IndexOfStill执行相同的操作,直到遇到一些问题。。。我把它们编辑成我的第一篇文章。很抱歉一直打扰到互联网,但我没有一个有效的调试功能(仍在试图找出它不起作用的原因)。不幸的是,到今天为止,我的问题仍然没有答案。康拉德的解决方案不起作用。我添加了更多信息,希望它能带来一个有效的解决方案,因为我自己似乎无法解决这个问题。
Const SitePattern = "(http://[^/]+)/"
Const IssuePattern = "issue=(\d+)"
Const PagePattern = "page=(\d+)"
Dim site = Regex.Match(input, SitePattern).Groups(1).Value
Dim issue = Regex.Match(input, IssuePattern).Groups(1).Value
Dim page = Regex.Match(input, PagePattern).Groups(1).Value
Dim result = String.Format("{1}/{2}_{3}", site, issue, page)
Const TestPattern = "^http://[^/]+/locale=[^&]+&mag=[^&]+&issue=[^&]+&page=[^&]+&template=[^&]+$"
Dim conformsToPattern = Regex.IsMatch(input, TestPattern)
If conformsToPattern Then
' Yes, go ahead. '
Else
' Nope, leave it unchanged. '
End If