473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

regex

hy,

i've got a simple question (for somebody who already knows the answer)
about regex:
i've a string like bla@bla@bla or bla@@bla
i like to check the @'s, but couldn't figure it out how to set zero or
more char's. (zero or one was easy).

thank's
rené

Aug 6 '07 #1
14 2219
rene,

Are you sure that a regular expression is the best option here? Why not
just call the Split method, passing the '@' character as the delimiter?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ohmmega" <sh****@gmx.atwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
hy,

i've got a simple question (for somebody who already knows the answer)
about regex:
i've a string like bla@bla@bla or bla@@bla
i like to check the @'s, but couldn't figure it out how to set zero or
more char's. (zero or one was easy).

thank's
rené
Aug 6 '07 #2
Hello ohmmega,
hy,

i've got a simple question (for somebody who already knows the answer)
about regex:
i've a string like bla@bla@bla or bla@@bla
i like to check the @'s, but couldn't figure it out how to set zero or
more char's. (zero or one was easy).
thank's
rené
What do you mean by 'I like to check the @'s'

Going from the fact that the rest of that sentence continues about soemthing
that sounds like a quantifier here's a short overview of the different quantifiers
available:
- ? - Zero or One
- * - Zero or More
- + - One or more
- {0,n} - Zero to n
- {n,} - n or more
- {n,m} - n to m

Take your pick ;)

Other than this being a question about regular expressions, you've not explained
what you wan to do with the end result. Regex is a pretty expensive tool
to use in terms of cpu power and in some scenario's memory consumption. Are
you sure it's the tool for the job? If you could explain a little about what
you're trying to achieve, we could potentially help you with a better solution.

Jesse
Aug 6 '07 #3
Hi,

I'm not sure what you mean with "check", what do you want to do when you
find a @ ?

"ohmmega" <sh****@gmx.atwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
hy,

i've got a simple question (for somebody who already knows the answer)
about regex:
i've a string like bla@bla@bla or bla@@bla
i like to check the @'s, but couldn't figure it out how to set zero or
more char's. (zero or one was easy).

thank's
rené
Aug 6 '07 #4
ajk
On Mon, 06 Aug 2007 13:49:32 -0000, ohmmega <sh****@gmx.atwrote:
>hy,

i've got a simple question (for somebody who already knows the answer)
about regex:
i've a string like bla@bla@bla or bla@@bla
i like to check the @'s, but couldn't figure it out how to set zero or
more char's. (zero or one was easy).

thank's
rené
this may help

http://sourceforge.net/projects/regulator/
Aug 6 '07 #5
i need to know if there are exactly 5 @'s with or without text in
beetween.
i thought compiled regex would be faster than splitting and .length.
nethertheless, if you guy's say "OH NO!!!", i've no reason to demand
on it.
Aug 7 '07 #6
On Aug 7, 9:23 am, ohmmega <sho...@gmx.atwrote:
i need to know if there are exactly 5 @'s with or without text in
beetween.
i thought compiled regex would be faster than splitting and .length.
nethertheless, if you guy's say "OH NO!!!", i've no reason to demand
on it.
How much do you care about performance in this case? How often are you
likely to call this? Have you measured the performance of Split and
found that it doesn't meet your requirements?

Jon

Aug 7 '07 #7
On 7 Aug., 11:00, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Aug 7, 9:23 am, ohmmega <sho...@gmx.atwrote:
i need to know if there are exactly 5 @'s with or without text in
beetween.
i thought compiled regex would be faster than splitting and .length.
nethertheless, if you guy's say "OH NO!!!", i've no reason to demand
on it.

How much do you care about performance in this case? How often are you
likely to call this? Have you measured the performance of Split and
found that it doesn't meet your requirements?

Jon
i need this about 200 times in a time critical application, so i just
want to have the best option.
i've not measured the time yet, but that's a good point - i will try
this next.

thanks so far
rené

Aug 7 '07 #8
Hello ohmmega,
On 7 Aug., 11:00, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
>On Aug 7, 9:23 am, ohmmega <sho...@gmx.atwrote:
>>i need to know if there are exactly 5 @'s with or without text in
beetween.
i thought compiled regex would be faster than splitting and .length.
nethertheless, if you guy's say "OH NO!!!", i've no reason to demand
on it.
How much do you care about performance in this case? How often are
you likely to call this? Have you measured the performance of Split
and found that it doesn't meet your requirements?

Jon
i need this about 200 times in a time critical application, so i just
want to have the best option.
i've not measured the time yet, but that's a good point - i will try
this next.
thanks so far
rené
If you still want to go the regex way, you basically have two options:

See if you can find a match for this:
^[^@]*(@[^@]*){5}$

Or do a Regex.Replace and replace everything that't not a @ with nothing
and measure the length of the text afterwards:

Regex.Replace(inputstring, "[^@]", "").Length 5

When using a regular expression, make sure you're usign a static instance
with the option RegexOption.Compiled set for performance reasons.

Like this

private static Regex rx = new Regex(pattern, RegexOptions.Compiled);

then reference this instance when using the expression.

Also add a static constructor to the class which calls rx.Match("");, that
way your performance needy code will not suffer the recompilation of the
regex.
You can also use a tool like The Regulator to generate an Assembly with the
compiled regex in there. This would give you the performance boost of not
having the regex compiled from the executable at all.

Even though this is a nice excercise in Regular expressions, I think that
simple string manupulations would be much faster...

public bool HasFiveAts(string input)
{
int count = 0;
foreach(char c in inputstring)
{
if (c == '@') { count++; }
// might even test for if (count 5) {return false;}, but you'd have
to test that for performance
}
return count == 5
}
Aug 7 '07 #9
A string is an array of char. I believe the fastedst way would be to loop
through the chars in the string, and count the '@' chars. Once you reach 5,
you return true. If you reach the end of the string without reaching 5, you
return false.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"ohmmega" <sh****@gmx.atwrote in message
news:11**********************@r34g2000hsd.googlegr oups.com...
i need to know if there are exactly 5 @'s with or without text in
beetween.
i thought compiled regex would be faster than splitting and .length.
nethertheless, if you guy's say "OH NO!!!", i've no reason to demand
on it.


Aug 7 '07 #10
On Aug 7, 12:46 pm, "Kevin Spencer" <unclechut...@nothinks.comwrote:
A string is an array of char. I believe the fastedst way would be to loop
through the chars in the string, and count the '@' chars. Once you reach 5,
you return true. If you reach the end of the string without reaching 5, you
return false.
Just to be clear: a string *isn't* an array of chars. It's a sequence
of characters, and there's a readonly indexer (as well as implementing
IEnumerable<char>) but it's not actually an array.

For example, you couldn't pass a string as an argument to a method
with a System.Array parameter.

Jon

Aug 7 '07 #11
On Aug 7, 10:19 am, ohmmega <sho...@gmx.atwrote:
i need this about 200 times in a time critical application, so i just
want to have the best option.
200 times in what space of time?
i've not measured the time yet, but that's a good point - i will try
this next.
That's definitely worth doing. In a little test application I wrote,
it took less than a second to check 2 *miilion* strings on my laptop.
Now, they were fairly short strings - you should test with real data -
but it's an indication.

Jon

Aug 7 '07 #12
Correct as usual, Jon. I should have said "a string encapsulates an array of
char, and can be treated just like one."

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@l70g2000hse.googlegro ups.com...
On Aug 7, 12:46 pm, "Kevin Spencer" <unclechut...@nothinks.comwrote:
>A string is an array of char. I believe the fastedst way would be to loop
through the chars in the string, and count the '@' chars. Once you reach
5,
you return true. If you reach the end of the string without reaching 5,
you
return false.

Just to be clear: a string *isn't* an array of chars. It's a sequence
of characters, and there's a readonly indexer (as well as implementing
IEnumerable<char>) but it's not actually an array.

For example, you couldn't pass a string as an argument to a method
with a System.Array parameter.

Jon

Aug 8 '07 #13
On Aug 8, 12:17 pm, "Kevin Spencer" <unclechut...@nothinks.comwrote:
Correct as usual, Jon. I should have said "a string encapsulates an array of
char, and can be treated just like one."
I think I'd still have quibbled: internally there isn't an array of
chars (unlike in Java, for instance) and you can't treat it as an
array of chars because you can always modify the contents of arrays,
whereas the string indexer is readonly.

There's always room for pedantry :)

Jon

Aug 8 '07 #14
Hi Jon,

I suppose pedantry has its place. These details are always useful to know.
In my case, I was simply talking about looping through the characters in the
string as an efficient way of counting the occurrences of a character.
However, it might have led to someone getting the wrong impression
concerning what ways one might be able to treat a string as if it were an
array of char. So, I don't take offense. I do think my solution was probably
the most efficient method for achieving the OP's goal, though. I hope that
the original problem has been solved.

--
HTH,

Kevin Spencer
Microsoft MVP

DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Aug 8, 12:17 pm, "Kevin Spencer" <unclechut...@nothinks.comwrote:
>Correct as usual, Jon. I should have said "a string encapsulates an array
of
char, and can be treated just like one."

I think I'd still have quibbled: internally there isn't an array of
chars (unlike in Java, for instance) and you can't treat it as an
array of chars because you can always modify the contents of arrays,
whereas the string indexer is readonly.

There's always room for pedantry :)

Jon

Aug 9 '07 #15

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

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
9
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
6
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
4
by: CJ | last post by:
Is this the format to parse a string and return the value between the item? Regex pRE = new Regex("<File_Name>.*>(?<insideText>.*)</File_Name>"); I am trying to parse this string. ...
0
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
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,...
0
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...

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.