简体   繁体   English

VBNet RegEx模式

[英]VBNet RegEx Pattern

I'm currently developing a SMS application in vbnet. 我目前正在vbnet中开发一个SMS应用程序。 One problem I encountered is when the sim card runs out of load. 我遇到的一个问题是当SIM卡耗尽时。 So I make a function that checks the balance if sending a message failed for 3 times. 因此,如果发送消息失败3次,我会创建一个检查余额的函数。 Now the problem is how can i parse or get the value from the string. 现在的问题是如何解析或从字符串中获取值。 I know this can be done in ReGex ( or you can suggest me if you have a better technique for this ). 我知道这可以在ReGex中完成( 或者你可以建议我,如果你有更好的技术 )。 This is the reply from the service provider: 这是服务提供商的回复:

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

I need to get the P1.00 from the string. 我需要从字符串中获取P1.00 And the possible valid format of the balance is: 并且可能的有效格式是:

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

So as you can see, the pattern has a monetary symbol of P (or sometimes Php ) and followed by a numeric value. 如您所见,该模式的货币符号为P (或有时为Php ),后跟数值。 Sometimes it has a white space between the monetary symbol and the value. 有时它在货币符号和值之间有一个空格。 The pattern also has two decimal places. 该模式还有两个小数位。 Now how can i do this using ReGex? 现在我如何使用ReGex做到这一点?

I cannot show some codes since I have no (even a single) idea where should i start from. 我无法显示一些代码,因为我没有(甚至一个)想法我应该从哪里开始。 I really need your help. 我真的需要你的帮助。

From a quick test in Expresso, this should do it for you: 通过Expresso中的快速测试,这应该为您做到:

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

And as explanation: 作为解释:

(?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

If you know that the response will be in that form, you might consider validating the whole message at the same time. 如果您知道响应将采用该格式,则可以考虑同时验证整个消息。

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()

Amount will include both the prefix and the number. 金额将包括前缀和数字。

Here's an explanation of the regex. 这是正则表达式的解释。

^                             (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)

Hope this helps. 希望这可以帮助。

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

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