Solved

Regular Expressions

Posted on 2013-01-18
2
233 Views
Last Modified: 2013-01-19
Hi All,

I'm trying to test a string to see if it contains another string. I can't use myString.Contains("string") as the string I'm searching for has to be on the LHS of an "=" symbol. For example,

from: "NumberOfGroups = 8"

I want to be able to test if the string contains the text NumberOfGroups and if true retrieve the value 8. I know I can use the split function using "=" as the delimiter. Does anyone know if I can achive this usinging regular expressions? Thank You.
0
Comment
Question by:naelyan
2 Comments
 
LVL 15

Expert Comment

by:navneethegde
ID: 38793489
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38794477
Try:

using System.Text.RegularExpressions;

...

string toFind = "some string";
string toSearchIn = "This is some string that is on the left of an = sign";
string pattern = Regex.Escape(toFind) + "(?=[^=]*=)";

bool contains = Regex.IsMatch(toSearchIn, pattern);

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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