Reputation: 81
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
. FYI there are few spaces in the string.
Thank you
Upvotes: 0
Views: 87
Reputation: 51330
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. I can't say more until I know the language you're using.
EDIT: In 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]
});
}
Upvotes: 2
Reputation: 174624
In 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'
Upvotes: 0