More regex questions

  • Thread starter Thread starter Trev
  • Start date Start date
T

Trev

Hi everyone,
I'm developing some code that will hopefully match one of the strings
stored in an array:

String [] attrTypes = {"/SCEQUIPMENT/", "/SCOINSTRUMENT/", "/SCPLIN/",
"/SCHVAC/", "/SCCAB/", "^(/EQUIPMENT/)", "/PIPE/", "^(/HVAC/)"};

for (int i=0; i<8; i++)
{
Match m = Regex.Match( args[0].ToString(), attrTypes );
// args[0] is an input string and contains (eg)
"SCEQUIPMENT (unfiltered)" etc.

if (m.Success)
{

but I never reach any of the code after m.Success. Can anyone please
suggest a solution?

TIA

Trev
 
Hello Trev,
I'm developing some code that will hopefully match one of the strings
stored in an array:

<snip>

The major problem here is that you seem to have copied the regular
expressions from Perl... the /.../ syntax is common there, for a variety
of reasons, but is not needed in C# (or .NET, for that matter). Get rid of
all the / characters in your expressions and you should see something
happening.


Oliver Sturm
 
Oliver said:
Hello Trev,


<snip>

The major problem here is that you seem to have copied the regular
expressions from Perl... the /.../ syntax is common there, for a variety
of reasons, but is not needed in C# (or .NET, for that matter). Get rid of
all the / characters in your expressions and you should see something
happening.


Thanks for your help Oliver, I really appreciate it (sorry I haven't
replied sooner, I have been on vacation): I admit that I started using
regex with Perl so the syntax must have stuck in my mind.

Just one more question: how do I only match to words at the start of a
line? So that
"EQUIPMENT" will only match with "EQUIPMENT (unfiltered)" but not
"GASKET for EQUIPMENT", "SCEQUIPMENT (unfiltered)" or "GASKET for
SCEQUIPMENT"

- I'm finding that a match is done anywhere in the string, but I can't
figure out how to just match at the start!

TIA

Trev
 
Hello Trev,
Just one more question: how do I only match to words at the start of a
line? So that
"EQUIPMENT" will only match with "EQUIPMENT (unfiltered)" but not
"GASKET for EQUIPMENT", "SCEQUIPMENT (unfiltered)" or "GASKET for
SCEQUIPMENT"

Use ^EQUIPMENT to match only at the start of the line (or the string,
rather).


Oliver Sturm
 
Use ^EQUIPMENT to match only at the start of the line (or the string,
rather).

Oliver Sturm

And be sure to set the appropriate regex option. Given

EQUIPMENT
EQUIPMENT

If your regex option includes RegexOptions.Multiline it will match twice.
Otherwise it will match only once
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Regex help needed 1
Regex - Matching URLS 2
Bug in Regex? 5
Regex Issues - Finding Qualified URLS 2
regex multiplication problem 3
REGEX help 2
Help with Regex 5
Rookie thoughts on Regex--useful but not complete 28

Back
Top