简体   繁体   English

从一个字符串中找到3个单词

[英]Find 3 words from a string

I am having problem how to use regex and it's been half day searching the commands, I hope anyone can help me with this issue. 我在使用正则表达式时遇到问题,已经搜索了半天的命令,希望有人可以帮助我解决此问题。

the string: 字符串:

ProductTable ChairStyleNewproductLocationUnited Kingdom

What I want is, get the words: Table Chair , Newproduct and the last is United Kingdom . 我要说的是: Table ChairNewproduct ,最后一个是United Kingdom FYI there are few spaces in the string. 仅供参考,字符串中的空格很少。

Thank you 谢谢

You'd have to post more input, because your data format is vague right now... 您必须发布更多输入信息,因为您的数据格式目前尚不明确...

But I'll try to answer anyway: 但是我还是会尝试回答:

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

Your info is in the 3 captured groups. 您的信息在3个捕获的组中。 I can't say more until I know the language you're using. 在知道您使用的语言之前,我无法说更多。

EDIT: In JS: 编辑:在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]
    });
}

In Python: 在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