SidFishes
asked on
Finding substring in middle or end
I'm putting together an address cleaner-up-er in coldfusion and I'm diving into regex for the first time today so be gentle..
I have a list of store names which can be like this
COMMON MKT FD COOP
COMMON MK FD
COMMON Nat MKT FD COOP
COMMON Nat FD mkt
I want to be able to clean these up to
COMMON MARKET FOOD COOP
COMMON MARKET FOOD
COMMON NATURAL MARKET FOOD COOP
COMMON NATURAL FOOD MARKET
coldfusion handles the replace, I just need to find the correct regex to feed it
I've tried vaious combinations similar to this
(\sMKT\.\s*$|\sMKT\s*$|\sM K\.\s*$|\s MK\s*$)
(\sNAT\.\s|\sNAT\s)
(\sFD\.\s|\sFD\s)
which works for
COMMON NATURAL FOOD MARKET
but doesn't for
COMMON NATURAL
and
(\sMKT\.\s|\sMKT\s|\sMK\.\ s|\sMK\s)
(\sNAT\.\s|\sNAT\s)
(\sFD\.\s|\sFD\s)
which works for
COMMON MARKET FOOD COOP
but doesn't for
COMMON MARKET FOOD
I understand why this is the case - the \sMKT\s*$ is end of line so if there is something after, it fails and \sMKT\s works when it's in the middle of the string but not at the end.
and if I do this
(\sMKT\.|\sMKT|\sMK\|\sMK)
(\sNAT\.|\sNAT)
(\sFD\.|\sFD)
it works but then I have issue accidentally replacing unwanted substrings
ie
LAMKIN Nat mk
becomes
LAMARKETIN Natural Market
so how do I find a substring that can be in the middle or the end of the string, without messing up unintended parts of the string.
I'm sure it's simple but I'm just pleased to have gotten this far....
note - I'm doing the replace one substring at a time as I have about 30 substitutions I want to make (ie: Road for Rd. etc)
<cfset storeName = rereplacenocase(StoreName, "(\sMKT\.\ s|\sMKT\s| \sMK\.\s|\ sMK\s)"," Market ")>
<cfset storeName = rereplacenocase(storeName, "(\sNAT\.\ s|\sNAT\s| \sNAT(?=,) )"," Natural ")>
I have a list of store names which can be like this
COMMON MKT FD COOP
COMMON MK FD
COMMON Nat MKT FD COOP
COMMON Nat FD mkt
I want to be able to clean these up to
COMMON MARKET FOOD COOP
COMMON MARKET FOOD
COMMON NATURAL MARKET FOOD COOP
COMMON NATURAL FOOD MARKET
coldfusion handles the replace, I just need to find the correct regex to feed it
I've tried vaious combinations similar to this
(\sMKT\.\s*$|\sMKT\s*$|\sM
(\sNAT\.\s|\sNAT\s)
(\sFD\.\s|\sFD\s)
which works for
COMMON NATURAL FOOD MARKET
but doesn't for
COMMON NATURAL
and
(\sMKT\.\s|\sMKT\s|\sMK\.\
(\sNAT\.\s|\sNAT\s)
(\sFD\.\s|\sFD\s)
which works for
COMMON MARKET FOOD COOP
but doesn't for
COMMON MARKET FOOD
I understand why this is the case - the \sMKT\s*$ is end of line so if there is something after, it fails and \sMKT\s works when it's in the middle of the string but not at the end.
and if I do this
(\sMKT\.|\sMKT|\sMK\|\sMK)
(\sNAT\.|\sNAT)
(\sFD\.|\sFD)
it works but then I have issue accidentally replacing unwanted substrings
ie
LAMKIN Nat mk
becomes
LAMARKETIN Natural Market
so how do I find a substring that can be in the middle or the end of the string, without messing up unintended parts of the string.
I'm sure it's simple but I'm just pleased to have gotten this far....
note - I'm doing the replace one substring at a time as I have about 30 substitutions I want to make (ie: Road for Rd. etc)
<cfset storeName = rereplacenocase(StoreName,
<cfset storeName = rereplacenocase(storeName,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
-not really -thanks! :)