Solved

RegEx Pattern Issue

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

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now