Solved

Finding substring in middle or end

Posted on 2013-01-25
2
253 Views
Last Modified: 2013-01-25
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*$|\sMK\.\s*$|\sMK\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 ")>
0
Comment
Question by:SidFishes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 38820257
\b(MKT\.|MKT\b|MK\.|MK\b)
0
 
LVL 36

Author Comment

by:SidFishes
ID: 38820292
man I hate it when it takes 20 minutes to type a q for a 1 line answer




-not really -thanks! :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
RegEx help in C#.NET 8 71
Date input validation (Regular Expressions) 1 61
Split string on commas but not when enclosed in parentheses 7 78
RegEx with optional part 4 88
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question