473,544 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ 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 "displaycop y" 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("displayc opy\" *.\"([^(]*)");

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 1736
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.goo glegroups.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 "displaycop y" 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("displayc opy\" *.\"([^(]*)");

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 "displaycop y" 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("displayc opy\" *.\"([^(]*)");

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 "displaycop y('" 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.co m>
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 "displaycop y('" 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 "displaycop y('" 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.StartsWit h("displaycopy( '") &&
line.EndsWith(" ')"))

Hope that helps.

--
Jon Skeet - <sk***@pobox.co m>
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.co m> 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.co m>
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 "displaycop y." 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.StartsWit h("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.Substrin g 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.Substrin g 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.co m>
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

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

Similar topics

9
10339
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 a string is a partial match to an re. Given an re of 'abcd and a bunch of other stuff' This is what i'm looking for:
9
380
by: MJ | last post by:
HI I want to know what is mean by regular expression in C Mayur
3
1054
by: ad | last post by:
I am learning regular expression. Are there any tools to test regular expression?
4
248
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 get "xxxxx<BR>xxxxx<BR>xxxxx<BR>xx". I think a regular expression would work for this. Now this problem: I have never done one before. How would I go...
8
1561
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 direction for creating such a regular expression or at least point me to the correct news group? Kindest regards, Michael
5
2982
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
416
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 seems not enough to explain by the way. Why no eliminate it ?
12
17578
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 , < ? / * ( ) & ^ % $ # ! ~ ` " ' this is my code:
5
411
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 question is, how would I write a regular expression that would change the value of #, given another number? For example, given the above variable, if I...
3
2331
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 anyone know a link besides the cuj january 2003 article ('A Different Interpretation of the Interpreter Design Pattern'). I am especially stuck with the...
0
7434
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7371
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7716
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5305
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4925
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3425
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3420
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
993
muto222
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.