簡體   English   中英

從一個字符串中找到3個單詞

[英]Find 3 words from a string

我在使用正則表達式時遇到問題,已經搜索了半天的命令,希望有人可以幫助我解決此問題。

字符串:

ProductTable ChairStyleNewproductLocationUnited Kingdom

我要說的是: Table ChairNewproduct ,最后一個是United Kingdom 僅供參考,字符串中的空格很少。

謝謝

您必須發布更多輸入信息,因為您的數據格式目前尚不明確...

但是我還是會嘗試回答:

^\s*Product(.+?)Style(.+?)Location(.+?)\s*$

您的信息在3個捕獲的組中。 在知道您使用的語言之前,我無法說更多。

編輯:在JS:

var re = /^\s*Product(.+?)Style(.+?)Location(.+?)\s*$/gm;
var inputString = "ProductTable ChairStyleNewproductLocationUnited Kingdom";
// Add another line of data
inputString += "\nProductDeskStyleOldproductLocationGermany";

var match;
while((match = re.exec(inputString))) {
    console.log({
        product: match[1],
        style: match[2],
        location: match[3]
    });
}

在Python中:

>>> import re
>>> exp = r'^Product(?P<product>(.*?))Style(?P<style>(.*?))Location(?P<loc>(.*?))$'
>>> i = "ProductTable ChairStyleNewproductLocationUnited Kingdom"
>>> results = re.match(exp, i).groupdict()
>>> results['loc']
'United Kingdom'
>>> results['product']
'Table Chair'
>>> results['style']
'Newproduct'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM