More regex questions (sorry!)

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

Trev

Hi,
If I had a string denoted by MyArray[5], how would I extract the number
in brackets for use in the programme later on? I've tried "\[(\d*)\]"
but all I get is a syntax error. What would be the usage for
using the number in the programme (so that I can check if the index is
within bounds)?

Talking of groupings, if I had a string like MyRef.MyArray.MyVal how
would I extract the strings
into an array for processing, so that Array[0] = MyRef, Array[1] =
MyArray, Array[2] = MyVal ?
There could be any number of strings, separated by periods.

TIA

Trev
 
Hello Trev,
If I had a string denoted by MyArray[5], how would I extract the number
in brackets for use in the programme later on? I've tried "\[(\d*)\]"
but all I get is a syntax error. What would be the usage for
using the number in the programme (so that I can check if the index is
within bounds)?

static void Main(string[] args) {
string testString = "MyArray[5]";
string regex = @"[^\[\]]+\[(?<index>\d+)\]";

Match match = Regex.Match(testString, regex);
if (match.Success)
Console.WriteLine("Index is " + match.Groups["index"].Value);

Console.ReadLine( );
}
Talking of groupings, if I had a string like MyRef.MyArray.MyVal how
would I extract the strings
into an array for processing, so that Array[0] = MyRef, Array[1] =
MyArray, Array[2] = MyVal ?
There could be any number of strings, separated by periods.

string splitString = "MyRef.MyArray.MyVal";
string[] parts = splitString.Split('.');
foreach (string part in parts) {
Console.WriteLine(part);
}


Oliver Sturm
 
Trev said:
Hi,
If I had a string denoted by MyArray[5], how would I extract the number
in brackets for use in the programme later on? I've tried "\[(\d*)\]"
but all I get is a syntax error. What would be the usage for
using the number in the programme (so that I can check if the index is
within bounds)?

Be careful because the \ is an escape character for regex, but also for C#.
You can use an unescaped literal @"..." as Oliver showed to get around that,
or if you need C# escape sequences like \t \r \n then any \ you want to
appear in the regex needs to be doubled (\\).
Talking of groupings, if I had a string like MyRef.MyArray.MyVal how
would I extract the strings
into an array for processing, so that Array[0] = MyRef, Array[1] =
MyArray, Array[2] = MyVal ?
There could be any number of strings, separated by periods.

TIA

Trev
 
As a newcomer to C#, I am extremely impressed by the abilities of Regex.
Thanks to all who have helped to plug a gap in my knowledge too!

(one question though - cheeky, I know! - how would I do a pattern match if
the ordering of alements didn't matter. So, if I wanted to match "pump",
"plop", or "pizzle", a positive match would be "pump, pizzle, plop", or
"plop, pizzle, pump")

Best wishes

Paul
 
You're not being very specific about your requirements. First, you said that
you wanted to match *any* of 3 values, by using the word "or." This means
that any *single* of the 3 values would constitute a match. Then you said
that any *combination* of the three values, separated by commas, as per your
example, would be a match. If you can clear up the requirement, we can help
you with the proper syntax.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Where there's a Will, there's a William.
 
I think he/she means a match on "a b c", "a c b", "b c a", so that it
doesn't matter on the ordering as long as all the search elements are
there.
 
Hello Kwebway,
one question though - cheeky, I know! - how would I do a pattern match if
the ordering of alements didn't matter. So, if I wanted to match "pump",
"plop", or "pizzle", a positive match would be "pump, pizzle, plop", or
"plop, pizzle, pump"

Well, for the words themselves, this expression matches either one of them:

pump|pizzle|plop

Now if you'd want to have that repeated, you'd put a quantifier on the
end, maybe like this (the + means 1 or more times)

(pump|pizzle|plop)+

This doesn't take care of the delimiter yet, so assuming you'd want to
have an optional comma delimiter followed by optional whitespace, you
might use an expression like this

((pump|pizzle|plop)(,\s*)?)+

Actually that question about the ordering comes up again and again... it
must be some wrong notion about regular expressions behind it. The fact of
the matter is that regular expressions often don't dictate order at all.
As soon as you use a quantifier (+ or * or ? or {m,n}) on a non-trivial
expression, like in the above examples, order is no longer enforced for
that part of the expression.


Oliver Sturm
 
i didn't read all the replies but i use regexlib.com frequently to steal
expressions and to test stuff. It's a good resource site.
 
Back
Top