473,471 Members | 4,648 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

More regex questions (sorry!)

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

Jan 11 '07 #1
8 1261
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
--
http://www.sturmnet.org/blog
Jan 11 '07 #2

"Trev" <t.*********@btinternet.comwrote in message
news:11**********************@o58g2000hsb.googlegr oups.com...
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

Jan 11 '07 #3
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

--
----
Home: http://www.paullee.com
Woes: http://www.dr_paul_lee.btinternet.co.uk/zzq.shtml
Jan 12 '07 #4
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.

"Kwebway Konongo" <pa**@pNaOuSlPlAeMe.comwrote in message
news:F7*********************@bt.com...
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

--
----
Home: http://www.paullee.com
Woes: http://www.dr_paul_lee.btinternet.co.uk/zzq.shtml

Jan 12 '07 #5
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.

Jan 12 '07 #6
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
--
http://www.sturmnet.org/blog
Jan 12 '07 #7
Thanks for all your help, friends.

Jan 16 '07 #8
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.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc

"Trev" <t.*********@btinternet.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Thanks for all your help, friends.

Jan 17 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: H | last post by:
Hi again ! I have 2 more questions, and I know theyre simple for you guys, so I hope you can help me. 1. Is there a c++ like command like system ? For example in c++ you can use system("cd...
22
by: Colin McGuire | last post by:
I apologize for posting yet another scrollbar question. Here is my code. All I want is for a diagonal line to appear from coordinates (0,0) to (width,height) in a usercontrol regardless of whether...
8
by: Just Me | last post by:
I want to use regular expressions to search a string, give the user the option of replacing, and then maybe replacing the data - using reg expressions for the search and the replace strings. ...
5
by: Omar | last post by:
Hi all...this is a good group so I'm sure you'll tolerate some more noobish questions... 1) I'm also learning to program flash movies while I learn to do python. How can one implement flash...
4
by: Trev | last post by:
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/",...
4
by: jobs | last post by:
1. How do I pass a subroutine a reference of an object? For example I have variable datef type datetime. I want to pass to pass datef the variable, not it's value to the sub? 2. In ADO.NET,...
2
by: Shawn B. | last post by:
Greetings, Lets say I have the following expression: (<A|ABBR|ADDRESS|APPLET(\s){1,}(.*?)>(.*?)</A|ABBR|ADDRESS|APPLET) Such that it'll match any HTML tag that opens with the above specified...
2
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07,...
2
by: Support Desk | last post by:
I am working on a python webcrawler, that will extract all links from an html page, and add them to a queue, The problem I am having is building absolute links from relative links, as there are so...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.