473,471 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regular Expression - More learning

In my effort to fully understand how to use the RegExp engine in c#.net
(VS2005), I have begun to tinker with it more since I was shown about
the ability the other day in a lengthy newsgroup discussion by Jon
Skeet
(http://groups.google.com/group/micro...b79e800a05dad).

I had not known about regular expressions in c# until that point.

Anyway, here is what I am trying to do:

Example Input: displaycopy('2,10,9,15')
Example Output: 2,10,9,15

Basically, trying to ignore the word "displaycopy" and anything that is
not a number or a comma. What I have come up with so far for a pattern
is this:

Regex re = new Regex("displaycopy\" *.\"([^(]*)");

However, it does not work as I am sure you've guessed by now.

I feel I *may* be close to using this properly, but still need a bit of
guidance with it. I am sure it is also possible that my attempt is
completely and utterly WRONG. But, this is part of learning and I am
sure the fine folks in this newsgroup (Jon included) will be able to
help me solve this puzzle.

Thanks in advance all.

-- Kirby

Dec 4 '05 #1
13 1732
Well, first, we have to determine what the exact format of your input string
is going to be. How you parse it depends upon the rules that the input
string follows. In other words, we don't need an example of an input string;
we need the business rules that define the format of the input string. For
example, without business rules, the input string could be:

"XC5];,&6TF , 3, TGI*//':;,8905XC@++"

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.

"O-('' Q)" <ca**************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
In my effort to fully understand how to use the RegExp engine in c#.net
(VS2005), I have begun to tinker with it more since I was shown about
the ability the other day in a lengthy newsgroup discussion by Jon
Skeet
(http://groups.google.com/group/micro...b79e800a05dad).

I had not known about regular expressions in c# until that point.

Anyway, here is what I am trying to do:

Example Input: displaycopy('2,10,9,15')
Example Output: 2,10,9,15

Basically, trying to ignore the word "displaycopy" and anything that is
not a number or a comma. What I have come up with so far for a pattern
is this:

Regex re = new Regex("displaycopy\" *.\"([^(]*)");

However, it does not work as I am sure you've guessed by now.

I feel I *may* be close to using this properly, but still need a bit of
guidance with it. I am sure it is also possible that my attempt is
completely and utterly WRONG. But, this is part of learning and I am
sure the fine folks in this newsgroup (Jon included) will be able to
help me solve this puzzle.

Thanks in advance all.

-- Kirby

Dec 4 '05 #2
O-('' Q) <ca**************@gmail.com> wrote:

<snip>
Anyway, here is what I am trying to do:

Example Input: displaycopy('2,10,9,15')
Example Output: 2,10,9,15

Basically, trying to ignore the word "displaycopy" and anything that is
not a number or a comma. What I have come up with so far for a pattern
is this:

Regex re = new Regex("displaycopy\" *.\"([^(]*)");

However, it does not work as I am sure you've guessed by now.


As Kevin said, a bit more information is required - don't get me wrong
though, the sample input is useful :)

A few questions:

1) Does the string always start with "displaycopy('" and end with "')"?
2) Will there ever be anything you want to ignore between the two?

If the answers are "yes" and "no", then regular expressions aren't what
you want here - you just want:

string useful = whole.Substring (13, whole.Length-15);

If the answers are "yes" and "yes", then a regular expression *might*
still be the best thing to use - although it could be complicated. I'm
hoping for the simple solution :)

Do you need to parse the results into integers, by the way? That could
affect which solution ends up being best...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 4 '05 #3
Sorry for my delayed reply. :)
As Kevin said, a bit more information is required - don't get me wrong
though, the sample input is useful :)
Ok, I will try.
A few questions:

1) Does the string always start with "displaycopy('" and end with "')"?
<script type="text/javascript"><!--
displaycopy('2,10,9,15'');
//--></script>

That is what the line always looks like. The only thing that changes is
the number inside the quotes. It could be 12,4,99,1 if that helps.
2) Will there ever be anything you want to ignore between the two?
Nothing between the two needs to be ignore. I just need the numbers and
the commas to output back to the user (me).
Do you need to parse the results into integers, by the way? That could
affect which solution ends up being best...


No, I just need to return a string. Integers are not necessary. Also,
your "simple" solution may be fine, hehe. I will give it a go and come
back later on to check on whether or not my further explaining helped
any.

Dec 4 '05 #4
O-('' Q) <ca**************@gmail.com> wrote:
Sorry for my delayed reply. :)
Delayed? It's been 40 minutes. In newsgroup terms, that's the
equivalent of replying before I've taken the next breath ;)
As Kevin said, a bit more information is required - don't get me wrong
though, the sample input is useful :)


Ok, I will try.
A few questions:

1) Does the string always start with "displaycopy('" and end with "')"?


<script type="text/javascript"><!--
displaycopy('2,10,9,15'');
//--></script>


Okay - so there'll be other stuff around it, we just need whatever's in
the displaycopy bit, right? Will there only be one displaycopy in the
whole document (or whatever you've got in the string)?
That is what the line always looks like. The only thing that changes is
the number inside the quotes. It could be 12,4,99,1 if that helps.


That's really handy.
2) Will there ever be anything you want to ignore between the two?


Nothing between the two needs to be ignore. I just need the numbers and
the commas to output back to the user (me).


Great :)
Do you need to parse the results into integers, by the way? That could
affect which solution ends up being best...


No, I just need to return a string. Integers are not necessary. Also,
your "simple" solution may be fine, hehe. I will give it a go and come
back later on to check on whether or not my further explaining helped
any.


If you've got the relevant line on its own already, that's going to be
the simplest solution - and if you're able to easily read the entire
HTML line by line, it's easy to check with something like:

if (line.StartsWith("displaycopy('") &&
line.EndsWith("')"))

Hope that helps.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 4 '05 #5
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
O-('' Q) <ca**************@gmail.com> wrote:
Sorry for my delayed reply. :)


Delayed? It's been 40 minutes. In newsgroup terms, that's the
equivalent of replying before I've taken the next breath ;)


<snip>

I should have added here, by the way, that I'm off to bed, so I'm
afraid you'll have to wait another 9 hours or so for another reply from
me. Of course, the beauty of newsgroups is that during those 9 hours
there are likely to be other people just as capable of answering your
question as I am :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 4 '05 #6
Have a good night, Jon. Thanks again for your input, it's been
extremely... enlightening. :)

Dec 4 '05 #7
>Okay - so there'll be other stuff around it, we just need whatever's in
the displaycopy bit, right? Will there only be one displaycopy in the
whole document (or whatever you've got in the string)?

That's really handy.
There is only one instance of "displaycopy." And I am glad I am giving
better information now. :)
If you've got the relevant line on its own already, that's going to be
the simplest solution - and if you're able to easily read the entire
HTML line by line, it's easy to check with something like:

if (line.StartsWith("displaycopy('") &&
line.EndsWith("')"))

Hope that helps.


Everything helps, Jon. Everything. :)

This was my effort to better understand Regular Expressions in this
environment. I am just hoping that I was at least in the general
geographical location with my crazy attempt in my original post, hehe.

Thanks again, all.

Dec 4 '05 #8
Ok, this one was solved. Thanks a TON for your hints and helpful code
examples.

Still, would like to see how the regular expression for this works. It
would really help me understand it some more. :)

The string.Substring routine was key. With a few small adjustments, it
did exactly what was needed and was easier to figure out than regex
was!

-- Kirby

Dec 4 '05 #9
O-('' Q) <ca**************@gmail.com> wrote:
Ok, this one was solved. Thanks a TON for your hints and helpful code
examples.

Still, would like to see how the regular expression for this works. It
would really help me understand it some more. :)

The string.Substring routine was key. With a few small adjustments, it
did exactly what was needed and was easier to figure out than regex
was!


Working out when to use regular expressions and when to use
Substring/IndexOf is a matter of taste, and some people like to use
regular expressions far more than I do. I'll have a look at the regex
though, and try to figure out what was wrong.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 5 '05 #10
O-('' Q) wrote:

<snip>
Anyway, here is what I am trying to do:

Example Input: displaycopy('2,10,9,15')
Example Output: 2,10,9,15
<snip>
Regex re = new Regex("displaycopy\" *.\"([^(]*)");


Just to go through your regular expression - it's actually looking for
a string starting with displaycopy then a double quote, then any number
of spaces, then any single character, then another double quote, then
any number of non-open-brackets, then a close bracket.

What I believe you want is:

Regex re = new Regex(@"displaycopy\('([^']*)'\)");

See if you can work out why :)

Jon

Dec 5 '05 #11
>What I believe you want is:

Regex re = new Regex(@"displaycopy\('([^']*)'\)");

See if you can work out why :)


Wow, I was way off, hehe.

I can see now where I went wrong for sure, given your example. It was
ignoring some of the objects in the string I needed and looking for
only parts of the same. If I am correct in saying so, that is what it
looks like to me now that I see how you formed the syntax.

I should really consider some classes on this or something. I can learn
a lot here, sure, but a class sounds like a fun and interesting
challenge. I should probably pick up a book while I am out today, too.
Can you recommend anything good for beginners?

Dec 5 '05 #12
"O-('' Q)" <ca**************@gmail.com> wrote:
Can you recommend anything good for beginners?


I recently learnt about them via this online tutorial:
<http://www.regular-expressions.info/>.
Dec 5 '05 #13
Wow, great link Cool Guy. Thanks a lot.

Dec 5 '05 #14

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

Similar topics

9
by: Ron Adam | last post by:
Is it possible to match a string to regular expression pattern instead of the other way around? For example, instead of finding a match within a string, I want to find out, (pass or fail), if...
9
by: MJ | last post by:
HI I want to know what is mean by regular expression in C Mayur
3
by: ad | last post by:
I am learning regular expression. Are there any tools to test regular expression?
4
by: unwantedspam | last post by:
Thank you in advance. I have a string that is variable length. Every five characters I need to put a "<BR>" in. So if I have the following: Dim Test as string = "xxxxxxxxxxxxxxxxx" I need to...
8
by: Michael Primeaux | last post by:
I'm somewhat new to regular expressions and am in need of assistance. I'd like to match on any OBJECT, EMBED, or APPLET element nested inside an <HTML> element. Would someone point me in the right...
5
by: teo | last post by:
I need to implement a boolean evaluation in a Regular Expression like this: (aaa AND bbb) OR (ccc AND ddd) (see the #3 case) - - - 1) If I need to match a single word only,
20
by: Asper Faner | last post by:
I seem to always have hard time understaing how this regular expression works, especially how on earth do people bring it up as part of computer programming language. Natural language processing...
12
by: adzir | last post by:
Hi, I need to validate password keyed in by the system users so that the password will contain only letters and numbers plus at least one capital letter. Exclude these symbols , < ? / * ( ) &...
5
by: laredotornado | last post by:
Hi, I have a span that contains text of the form var spanHtml = "My Tab Content (7)"; The content is guaranteed to end with a string of the form "(#)" where "#" is a whole number. My...
3
by: gast128 | last post by:
Dear all, I was looking for a c++ implementation of regular expressions using the Interpreter Design Pattern. GOF only references a Smaltalk implementation which I can not read :(. Does...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.