|
|
U can use
^[a-zA-Z0-9]+ (\\d+ | [a-zA-Z]+)
|
|
|
|
|
This is not a good regex task - it isn't strictly pattern matching, it's a counting job, which regex is pretty bad at.
While you can do it it would be horribly complex, and if you decided at a later date that you wanted to include special characters in there it would be a real PITA to maintain.
Instead, do it in code: Use two regexes - one to return all the digits, and one to return all the alpha characters. You then check that the length of both is greater than zero.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Assuming you're using the .NET RegEx class, you can use a zero-width positive lookahead assertion[^]:
^(?=.*?\d)(?=.*?\D)\w{2,}$
This means:
- You're matching the entire string -
^ and $ - The string must consist of at least two alpha-numeric characters -
\w{2,} - The string must contain any number of characters followed by a digit -
(?=.*?\d) - The string must contain any number of characters followed by a character that is not a digit -
(?=.*?\D)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
please, tell me this has a bug:
"^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((19|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$"
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
That's a very long expression.
This is a shorter expression that validate dates:
^\d{4}[- /.](0?[1-9]|1[0-2])[- /.](0?[1-9]|[12][1-9]|3[01])$
Expresso[^] is a good tool to create regular expressions.
In some cases, my signature will be longer than my message...
<em style="color:red"> <b>ProgramFOX</b></em> ProgramFOX
|
|
|
|
|
thanks for the reply, we actually found a bug on that expression when entering 01/01/2013 (dd/MM/yyyy), for some reason, this value was rejected...
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Sentenryu wrote: thanks for the reply
You're welcome!
Sentenryu wrote: dd/MM/yyyy
My expression is for yyyy/MM/dd. An expression for dd/MM/yyyy:
^(0?[1-9]|[12][1-9]|3[01])[- /.](0?[1-9]|1[0-2])[- /.]\d{4}$
In some cases, my signature will be longer than my message...
<em style="color:red"> <b>ProgramFOX</b></em> ProgramFOX
|
|
|
|
|
Its much shorter yes, but it's overly simplistic.
It says 31st Feb is a valid date
|
|
|
|
|
There's a difference between a valid date and a valid date format. A regex is good for checking formats, but I don't think it works for checking whether a day exists.
In some cases, my signature will be longer than my message...
<em style="color:red"> <b>ProgramFOX</b></em> ProgramFOX
|
|
|
|
|
Maybe it checks if you have to go to work too. AFAIK 1.1 is work-free.
Greetings - Jacek
|
|
|
|
|
Hi there,
I've just read the great "30 Minute Regex Tutorial", by Jim Hollenhorst. A brilliant piece of writing. Congratulations, Jim. Expresso is also a really superb tool.
I would need to go deeper into the advanced aspects of regexp, such as the examples in Table 6 (Table 6. Everything we left out.) in that tutorial.
I would like to hear some suggestions about what way I can go. Thanks a lot!
Cheers, Manuel
|
|
|
|
|
msoutopico wrote: Expresso is also a really superb tool. Yes, indeed it is.
The best suggestion I have is to find (or invent) some interesting problems, then play with Expresso to solve them (in as many ways as you can think of).
Having said that, remember that regex is not the universal solution for string processing. There are many cases where regex is not the appropriate tool.
(Remember the old saw "When all you have is a hammer, everything looks like a nail." Don't let regex become your hammer.)
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Thanks Peter.
Any suggestions for regexp advanced exercises?
Peter_in_2780 wrote: There are many cases where regex is not the appropriate tool. For example?
I would depend on the case, I suppose.
Cheers, Manuel
|
|
|
|
|
sir
i am doing a project, i want to match a string with regex which will match the whole string and it dosent allow two white spaces concerrently on after the other and also it dosent allow white spaces at starting and ending of string, any number of white spaces is allowable but not concurrently one after the other
|
|
|
|
|
I'm not going to write it out for you, but how about
[beginning of string][whitespace] OR [whitespace](two or more) OR [whitespace][end of string] Any match and it's bad.
Or, depending on how you want to use it, you can invert the test.
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
i didint get what you have told
|
|
|
|
|
He tried to re-define your problem so that it can be easily translated to regular expressions:
Quote: [beginning of string][whitespace] OR [whitespace](two or more) OR [whitespace][end of string] He wanted you to write it out yourself. However, I will guide you to the solution. Using Peter's suggestion, you will get:
^\s OR \s{2,} OR \s$
In Perl, this may look like:
if ($str =~ /^\s/ || $str =~ /\s{2,}/ || $str =~ /\s$/) {
print "String '$str' contains invalid white space sequence(s).";
}
|
|
|
|
|
I have a path and I want to get the part of a path before my match:
string s1= @"c:\temp\lev1\lev2\lev3\MYWORD\lev5\lev6";
string mtch ="MYWORD"
knowing
s1 and
mtch I want to get the part of path before mtch, so @"c:\temp\lev1\lev2\lev3\".
I tried with RegEx but I don't know how to be elegant.. I'm only able to find MYWORD position and then use the substring.
Is there a smarter solution?
|
|
|
|
|
Regex seems overkill for this, but you can do it with a zero-width positive lookahead assertion[^]:
string s1 = @"c:\temp\lev1\lev2\lev3\MYWORD\lev5\lev6";
string mtch = "MYWORD";
string pattern = "^.*(?=" + Regex.Escape(mtch) + ")";
Console.WriteLine(Regex.Match(s1, pattern).Value);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
Also look at the various overloads of Regex.Split() , especially if the initial "match" is more than just a simple string.
|
|
|
|
|
I have a string like:
string s1 = "dog().Cat(\"Happy\")";
I want to:
1) count the number of round brackets '(', I expect as result 2
2) get the text inside, I expect "\"Happy"\", which appear in console output as "Happy"
No idea for 1. I try the following for 2
string k = Regex.Match(s1,@"\((\w+)\)").Groups[1].Value;
But It fails in understanding the \" character
Any Idea?
|
|
|
|
|
You're nearly there:
string k = Regex.Match(s1, @"\(""(\w+)""\)").Groups[1].Value;
For #1, unless you need to ensure that the brackets are balanced, Regex is overkill. Add using System.Linq; to your file, and try:
int count = s1.Count(c => c == '(');
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks you get the point.
|
|
|
|