Regular Expression: replace method

W

William Morris

Been pounding on this for an hour and eating up my day's productivity, so I
thought I'd throw a question out there. (I'm also reading through the
Google archives to try to find this answer...)

Windows 2000 Small Business Server

Using regular expressions for the first time, and having difficulty with the
replace method. We're searching through a list of vehicle models, trying to
replace what we get from the client data with correctly propercased
text...ie..we get FORD F150 and replace it with Ford F150; CHEVROLET ES 300
and replace it with Chrevrolet ES 300; ACURA MDX >> Acura MDX. The usual
propercase algorithms don't work because of the multiple capitals in some of
the model names: we don't want Acura Mdx.

The test method always works. The problem we're having is that, under
certain circumstances, the replace method is replacing too much. We've
created a table of capitals as they relate to models: ES; MDX; XL; SL; and
so on. The pattern we're using is this:

\b varCapitals (\b|\d) << with spaces for clarity

which I read to mean "a word boundary followed by our capitals followed by
either a word boundary or a number."

And now a sample of our output, assuming a capital combination of "ES".

Shadowes >> Shadowes (good)
Shadow es >> Shadow ES (good)
es 300 >> ES 300 (good)
es300 >> ES00 (failed - we want ES300)

What needs to change in the pattern? Thanks for taking the time!
 
W

William Morris

Well, we've got the first problem solved, and introduced another.

TestString = "300 mdx 300"
Pattern = (\b|\d)MDX(?=\b|\d)
Success = 300 MDX 300

TestString = "300mdx 300"
Pattern = (\b|\d)MDX(?=\b|\d) << same as prev
Failed = 30MDX 300 << "30" instead of "300" at the beginning of the string.

TestString = "300mdx 300"
Pattern = (?!\b|\d)MDX(?=\b|\d)
Success = 300MDX 300

TestString = "300 mdx 300"
Pattern = (?!\b|\d)MDX(?=\b|\d) << same as prev
Failed = 300 mdx 300 << replace failed

Help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,831
Members
47,374
Latest member
anuragag27

Latest Threads

Top