?
Solved

RegEx Pattern Issue

Posted on 2014-07-23
2
Medium Priority
?
128 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 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
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

770 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