Solved

RegEx Pattern Issue

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

932 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

15 Experts available now in Live!

Get 1:1 Help Now