![](/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.