Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex VBNet正则表达式模式_Regex_Vb.net - Fatal编程技术网

Regex VBNet正则表达式模式

Regex VBNet正则表达式模式,regex,vb.net,Regex,Vb.net,我目前正在vbnet中开发一个SMS应用程序。我遇到的一个问题是sim卡负载不足。因此,我做了一个函数,如果发送消息失败3次,它会检查余额。现在的问题是如何解析或从字符串中获取值。我知道这可以在正则表达式中完成(或者如果你有更好的技巧,你可以建议我)。这是服务提供商的回复: Your current balance as of 02/15/2012 00:17 is P1.00 valid til... 我需要从字符串中获取P1.00。余额的可能有效格式为: P1.50 P10.00 P10

我目前正在vbnet中开发一个SMS应用程序。我遇到的一个问题是sim卡负载不足。因此,我做了一个函数,如果发送消息失败3次,它会检查余额。现在的问题是如何解析或从字符串中获取值。我知道这可以在正则表达式中完成(或者如果你有更好的技巧,你可以建议我)。这是服务提供商的回复:

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
  • 菲律宾比索1.00
  • Php10.50
如您所见,该模式有一个货币符号
p
(或者有时是
Php
),后跟一个数值。有时它在货币符号和价值之间有一个空白。该模式还有两个小数位。现在我如何使用正则表达式来实现这一点


我不能显示一些代码,因为我不知道从哪里开始。我真的需要你的帮助。

通过在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()
Dim pattern=new Regex(“^您截至\d{2}/\d{2}/\d{4}\d{2}:\d{2}的当前余额是(?P(?:hp)?\s*\d+\.\d{2}的有效til”)
尺寸匹配=模式匹配(输入)
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)
^(字符串的开头)
截至的当前余额(文字)
\d{2}/\d{2}/\d{4}\d{2}:\d{2}(日期和时间)
is(文字字符串“is”)
(?(开始命名捕获组)
P(字面上的“P”)
(?:hp)?(可选非捕获“hp”)
\s*(0个或更多空白字符)
\d+\.\d{2}(1个或多个数字,点,两个数字
)(结束命名为捕获组)
有效til(文字文本)

希望这有帮助。

这里有一个屏幕截图:。到目前为止还不错,但是为什么没有选择
Ph
呢?如果它总是p或Php,你可以使用
p(?:hp)?
而不是
[p | Php]
。它没有选择
Ph
,因为它在p而不是Php上匹配。