Solved

RegEx Pattern Issue

Posted on 2014-07-23
2
119 Views
Last Modified: 2014-10-23
The issue I have is that I am trying to replace words in a text string which is fairly straight forward except for one small twist the .S .T P. in the names.  The are words that I am replace in the text strings that match part but not all of the name.  For example.  FLBOX58 and FLBOX58.T are both valid.  Obviously I only want FLBOX58 to match when the .T is not on the end.  How can I make sure the word patterns match exactly?

P.BASEPRICE.S DLINE.T FLBOX58.T
0
Comment
Question by:WH_Tech
[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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40215858
You would use a negative lookahead for this:

Regex reg = new Regex("FLBOX58(?!\.S|\.T|\.P)");

Open in new window


The lookahead part is:

(?!\.S|\.T|\.P)

The ! means it's negative. What the lookahead does is that once the "main" pattern matches, the lookahead peeks forward to compare what follows. Since the lookahead is negative, we are saying that the items listed within the lookahead should not be matched. In other words, once we match "FLBOX58", we ensure that we do not find a .S, .T, or a .P. If one is found, then we don't have an overall match.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40215910
No points please.

Tidying up kaufmed's pattern, if you prefer it this way:
Regex reg = new Regex("FLBOX58(?!\.[STP])");

Open in new window

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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,…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
Suggested Courses

739 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