使用正则表达式识别字符串中的单词

时间:2015-07-03 10:39:03

标签: regex perl

我有以下文字:

your salary $4500 is deposited in account ABC09-234-1234
your salary $4500 is deposited in account abc09-234-1234

我尝试使用(\d+)|([A-Z0-9-]+)正则表达式,但它不能使用小写字母。

我想要抓取$4500Account Number。 请帮帮我。

4 个答案:

答案 0 :(得分:3)

两个选项:

  • 使用i
  • 使用/(\$\d+)|([A-Z0-9-]+)$/i 正则表达式修饰符,使其不区分大小写。

像这样:

(\$\d+)|\b([A-Z0-9]*-[A-Z0-9]*)\b

编辑:根据您的行尾'不是一个坚定的锚:

-

这会捕获必须包含/(\S*\d)/ 符号的字母和数字序列。

但你也许可以简化 - 如果你认为你感兴趣的的东西是结尾的子串(你的例子就是这样)

val b = sc.parallelize(Array(1,2,3))
val c: Int = 4
def add (x:org.apache.spark.rdd.RDD[Int]) = { x.map( n => n + c) }

add(b).collect()

两条线都匹配:

Demo

答案 1 :(得分:2)

您可以使用以下正则表达式:

$4500

请参阅demo

这个正则表达式将匹配ABC09-234-1234的子串(字符串中的任何位置)和{{1}} - 就像字符串一样。

答案 2 :(得分:2)

<强>假设

  • Money值始终以$开头,不包含任何空格 其他非数字字符
  • 帐号始终格式为3&#39;块&#39;以-
  • 分隔

<强>解决方案

(\$\d+)|([A-Za-z0-9]+-[A-Za-z0-9]+-[A-Za-z0-9]+)

Here is a working example

答案 3 :(得分:1)

目前还不是很清楚你想要什么,但这个解决方案可以帮到你

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