2017-04-07 100 views
-2

突然想到说,我们有以下格式的字符串匹配的行:正则表达式使用一个或多个单词的搜索键

“第1部分第2部分:第3部分”

第一部分第2部分是大多数时间一个单词(包括小写和大写字母)和第3部分可以是数字和字符(包括标点符号)的任意组合。另外,在:

之前或之后可以有多个空格。我将编写一个正则表达式来匹配这样的字符串。例如,如果一个样本的字符串如:

String sample1 = "Total Amount: $1,000"; 
String sample2 = "Company: Google Inc."; 

搜索一键像,或总量我想将SAMPLE1字符串相匹配。还搜索一个关键像公司我想匹配sample2字符串。

编辑:

其结果是,如果我们有像上面的样品一个字符串,通过搜索 “总”,“金额”,或“总金额”我们应该回到样品 字符串作为匹配。并且,它是:重要即:1)不仅行应该包含密钥,而且它也应该遵循该行的指定格式和2)用户输入密钥

这是我现在的正则表达式:

String reg = "[tTOoTtAaLl0-9.,\/#!$%\^&\*;:{}=\-_`~( ) ]+:[A-Za-z0-9.,\/#!$%\^&\*;:{}=\-_`~( ) ]+"; 

不过,我正则表达式不整行匹配。值得指出的是,我使用Java,并假设示例字符串存储在示例中,我使用sample.matches(reg)来检查字符串是否匹配。

+0

在“part1和part2是单词的大部分时间”中,大部分时间你的意思是什么? –

+0

java正则表达式:'^ \\ w * \\ W * \\ w * \\ W * \\ d * \\ W * \\ d *'这与以下两个例子匹配:'总金额:$ 1,000' 'asdasd asdasd:$ 121.11'只要在':'之前只有2个单词就可以工作。 如果你只是想检查一个字符串是否包含数量或总数,你可以使用'.contains()'方法 – XtremeBaumer

+0

你的要求不清楚,acc。对于你写的内容,你可以使用'“^(\\ w +)\\ s +(\\ w +)\\ s *:\\ s *(。*)”'。 –

回答

0

不知道如果我正确理解你的要求,但在这里,你必须根据你的数据,你想要得到什么,我猜2个例子:

1)示例只得到的美元金额:https://regex101.com/r/7P2Bad/3

2)示例为获得相匹配的完整的字符串:https://regex101.com/r/7P2Bad/2

考虑到使用Java(不相同的语法regex101.com了)正则表达式,你应该逃避一些字符。

与同类个案工作:

aaa bbb! : $34 
Total Amount: $1,000 
njhjh hjh: $33 
Google Google1: $100 

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class Test { 

public static void main(String[] args) { 
    // Case1: Getting full string 
    Pattern p1 = Pattern.compile("(.*\\s?.*:\\s?\\$.*)"); 
    Matcher matcher1 = p1.matcher("Total Amount: $1,000"); 
    if (matcher1.matches()) { 
     System.out.println("Full string matched: " + matcher1.group(1)); 
    } 

    // Case2: Getting only the amount of dollars 
    Pattern p2 = Pattern.compile(".*\\s?.*:\\s?\\$?(.*)"); 
    Matcher matcher2 = p2.matcher("Total Amount: $1,000"); 
    if (matcher2.matches()) { 
     System.out.println("Amount only from matched string: " + matcher2.group(1)); 
    } 
} 
} 

输出:

Full string matched: Total Amount: $1,000 
Amount only from matched string: 1,000 

EDITED后评论:

然后,阅读您的评论后,你想是这样的:

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class test { 
public static void main(String[] args) { 
    String userKey = "Amount"; 
    System.out.println("Full string matched: " + getMatch(userKey)); 

    userKey = "total"; 
    System.out.println("Full string matched: " + getMatch(userKey)); 
} 

private static String getMatch(String key) { 
    // Case: Getting full string for userKey (case insensitive) matches 
    Pattern p1 = Pattern.compile(String.format("(?i)(.*%s.*\\s?.*:\\s*?\\$.*)", key)); 
    Matcher matcher1 = p1.matcher("Total Amount: $1,000"); 
    if (matcher1.matches()) { 
     return matcher1.group(1); 
    } 
    return ""; 
} 
} 

输出:

Full string matched: Total Amount: $1,000 
Full string matched: Total Amount: $1,000 

说明:

  • (我?)>搜索不区分大小写
  • %S>只要把使用的String.format是%s的占位符userKey变种方法
+0

我想要的东西是:如果用户输入一个关键字,如“金额”或“总计”或“总金额”,则“总金额:$ 1,000”行将作为匹配返回。唯一缺少的正则表达式是搜索关键字,这是我的主要问题。我们应该如何在正则表达式中包含这些键来查找完整匹配? – Pedram

+0

我已经添加了另一个解决方案,我猜“匹配”您的需求。希望它有帮助:) – exoddus

+0

感谢您的回复。只是一个简单的问题:如果我们这样做:“公司:谷歌公司”和搜索键是“公司”,似乎目前的正则表达式不适用于这一个。换句话说,part3不一定是带美元符号的数字。编辑:只是想:我们应该简单地删除'\\ $' – Pedram

相关问题