Эта строка автоматически генерируется приложением, к которому я не могу получить доступ или изменить:
«http://www.site.com/locale=euen&mag=testit&issue=322&page=5&template=testit-t1"
Мне нужно изменить строку на
"http://www.site.com/322_5"
где:
- http://www.site.com/ взято из предыдущей строки
- 322 исходит от эмиссии = 322
- 5 происходит от page = 5 в первой строке.
Но это может быть и другая проблема или номер страницы, заранее неизвестно.
Как мне это сделать в VB.NET? (Это должен быть VB.NET) Я пробовал что-то с разделением и сравнением, но я разбираюсь с разбивкой строк. Помощь будет приветствоваться!
РЕДАКТИРОВАТЬ :
после попытки решения Конрада ниже, я получаю ошибку, когда пытаюсь пропустить строку через него. Все остальные URL-адреса продолжают работать нормально, но как только я добавляю один в формате, который необходимо преобразовать, он ошибается.
Я подозреваю, что это потому, что функция преобразования является частью еще одной функции, и я делаю что-то не так, когда пытаюсь вставить функцию регулярного выражения.
Это полная функция:
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
Проблема в следующей строке?
sUrl = String.Format("{1}/{2}_{3}", sSite, sIssue, sPage)?