Solved

RegEx Pattern Issue

Posted on 2014-07-23
2
115 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…

808 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