2017-04-07 43 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

相關問題