![](/img/trans.png)
[英]Regex does not identify '#' for removing “#' from words starting with ”#'
[英]Identify words from the string with regex
我有以下文字:
your salary $4500 is deposited in account ABC09-234-1234
your salary $4500 is deposited in account abc09-234-1234
我尝试使用(\\d+)|([A-Z0-9-]+)
正则表达式,但不能使用小写字母。
我想提取$4500
和Account Number
。 请帮我。
两种选择:
[A-Za-z0-9]
。 i
regex修饰符,使其不区分大小写。 像这样:
/(\$\d+)|([A-Z0-9-]+)$/i
编辑:鉴于您的“行尾”不是坚定的锚点:
(\$\d+)|\b([A-Z0-9]*-[A-Z0-9]*)\b
这将捕获字母和数字的序列,这些序列必须包含-
符号。
但是您可以简化一下-如果您假设唯一感兴趣的是以数字结尾的子字符串(您的示例就是)
/(\S*\d)/
将在两条线上都匹配:
您可以使用以下正则表达式:
(?<salary>\$\d+)|\b(?<account>[a-zA-Z0-9]+(?:-[0-9]+)+)
观看演示
此正则表达式将匹配$4500
的子字符串(字符串中的所有位置)和ABC09-234-1234
的字符串。
假设 :
$
开头,并且不包含任何其他非数字字符的空格 -
分隔 解决方案 :
(\$\d+)|([A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+)
不清楚您想要什么,但是此解决方案可能会为您提供帮助
use strict;
use warnings;
while ( <DATA> ) {
my @words = grep /\d/, split;
print "@words\n";
}
__DATA__
your salary $4500 is deposited in account ABC09-234-1234
your salary $4500 is deposited in account abc09-234-1234
$4500 ABC09-234-1234
$4500 abc09-234-1234
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.