Regular Expression: replace method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Morris

    Regular Expression: replace method

    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!


    --
    William Morris
    Product Development, Seritas LLC
    Kansas City, Missouri


  • William Morris

    #2
    Re: Regular Expression: replace method

    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!



    "William Morris" <news.remove.th is.and.the.dots @seamlyne.com> wrote in
    message news:c252j9$1oc 84a$1@ID-205671.news.uni-berlin.de...[color=blue]
    > Been pounding on this for an hour and eating up my day's productivity, so[/color]
    I[color=blue]
    > 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[/color]
    the[color=blue]
    > replace method. We're searching through a list of vehicle models, trying[/color]
    to[color=blue]
    > 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[/color]
    300[color=blue]
    > 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[/color]
    of[color=blue]
    > 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!
    >
    >
    > --
    > William Morris
    > Product Development, Seritas LLC
    > Kansas City, Missouri
    >
    >[/color]


    Comment

    Working...