簡體   English   中英

VBNet RegEx模式

[英]VBNet RegEx Pattern

我目前正在vbnet中開發一個SMS應用程序。 我遇到的一個問題是當SIM卡耗盡時。 因此,如果發送消息失敗3次,我會創建一個檢查余額的函數。 現在的問題是如何解析或從字符串中獲取值。 我知道這可以在ReGex中完成( 或者你可以建議我,如果你有更好的技術 )。 這是服務提供商的回復:

Your current balance as of 02/15/2012 00:17 is P1.00 valid til...

我需要從字符串中獲取P1.00 並且可能的有效格式是:

  • P1.50
  • P10.00
  • P100.00
  • P 100.75
  • P 1.00
  • Php 1.00
  • Php10.50

如您所見,該模式的貨幣符號為P (或有時為Php ),后跟數值。 有時它在貨幣符號和值之間有一個空格。 該模式還有兩個小數位。 現在我如何使用ReGex做到這一點?

我無法顯示一些代碼,因為我沒有(甚至一個)想法我應該從哪里開始。 我真的需要你的幫助。

通過Expresso中的快速測試,這應該為您做到:

(?i)[P|Php](?: ?)(\d*\.\d\d) valid

作為解釋:

(?i) == case insensitive
[p|php] == p or php
(?: ?) == a space, 0 or 1 repetitions, but do not capture the group
(\d*\.\d\d) == a digit any amount, followed by a . followed by two digits

如果您知道響應將采用該格式,則可以考慮同時驗證整個消息。

Dim pattern = new Regex("^Your current balance as of \d{2}/\d{2}/\d{4} \d{2}:\d{2} is (?<amount>P(?:hp)?\s*\d+\.\d{2}) valid til")
Dim match = pattern.Match(input)
Dim amount = match.Groups("amount").ToString()

金額將包括前綴和數字。

這是正則表達式的解釋。

^                             (Beginning of string)
Your current balance as of    (Literal text)
\d{2}/\d{2}/\d{4} \d{2}:\d{2} (Date and time)
 is                           (Literal string " is ")
(?<amount>                    (Begin named capture group)
P                             (Literal "P")
(?:hp)?                       (Optional non-capturing "hp")
\s*                           (0 or more whitespace characters)
\d+\.\d{2}                    (1 or more numbers, dot, two numbers
)                             (End named capture group)
 valid til                    (Literal text)

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM