繁体   English   中英

带有 XE 货币的正则表达式

[英]regex with XE currency

伙计们,我正在尝试使用 VB.Net 制作我的个人应用程序

并且我的所有代码都运行良好,除了一件事,那就是正则表达式

我想得到这个值

我需要的突出价值

从这个网址

我试过这个正则表达式:

("([0-9]+.+[1-9]+ (SAR)+)")

并且效果不佳(仅适用于某些货币,但不适用于所有货币)。

所以伙计们,你能帮助完美的正则表达式吗?

***更新:这里是完整的功能代码:

Private Sub doCalculate()
    ' Need the scraping
    Dim Str As System.IO.Stream
    Dim srRead As System.IO.StreamReader
    Dim strAmount As String
    strAmount = currencyAmount.Text

    ' Get values from the textboxes
    Dim strFrom() As String = Split(currecnyFrom.Text, " - ")
    Dim strTo() As String = Split(currecnyTo.Text, " - ")

    ' Web fetching variables
    Dim req As System.Net.WebRequest = System.Net.WebRequest.Create("https://www.xe.com/currencyconverter/convert.cgi?template=pca-new&Amount=" + strAmount + "&From=" + strFrom(1) + "&To=" + strTo(1) + "&image.x=39&image.y=9")
    Dim resp As System.Net.WebResponse = req.GetResponse

    Str = resp.GetResponseStream
    srRead = New System.IO.StreamReader(Str)


    ' Match the response
    Try
        Dim myMatches As MatchCollection
        Dim myRegExp As New Regex("(\d+\.\d+ SAR)")

        myMatches = myRegExp.Matches(srRead.ReadToEnd)

        ' Search for all the words in the string
        Dim sucessfulMatch As Match
        For Each sucessfulMatch In myMatches
            mainText.Text = sucessfulMatch.Value
        Next
    Catch ex As Exception
        mainText.Text = "Unable to connect to XE"
    Finally
        ' Close the streams
        srRead.close()
        Str.Close()
    End Try
    convertToLabel.Text = strAmount + " " + strFrom(0) + " Converts To: "
End Sub

谢谢。

你应该使用这个正则表达式。

正则表达式: (\\d+\\.\\d+ SAR)

解释:

  • \\d+查找多个数字。

  • \\.\\d+查找十进制数字。

  • SAR匹配文字字符串SAR ,它是您的货币单位。

Regex101 演示


我试过这个正则表达式:

("([0-9]+.+[1-9]+ (SAR)+)") 并且效果不佳(仅适用于某些货币,但不是全部)。

您在这里所做的是multiple digits匹配multiple digits anything multiple digits SAR multiple times.

您需要获取首先出现的货币值。 因此,您需要更换

myMatches = myRegExp.Matches(srRead.ReadToEnd)
' Search for all the words in the string
Dim sucessfulMatch As Match
For Each sucessfulMatch In myMatches
    mainText.Text = sucessfulMatch.Value
Next

有以下几行:

Dim myMatch As Match = myRegExp.Match(srRead.ReadToEnd)
mainText.Text = myMatch.Value

我还建议使用以下正则表达式

\b\d+\.\d+\p{Zs}+SAR\b

解释:

  • \\b - 词边界
  • \\d+ - 1+ 位数
  • \\. - 一个文字点
  • \\d+ - 1+ 位数
  • \\p{Zs}+ - 1 个或多个水平空白
  • SAR\\b - 全字SAR

暂无
暂无

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

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