|
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.
|
|
|
|
|
Hi,
I'm going nuts. I've got heaps of html-pages, that have to be adjusted, since there are "a href" tags within "pre" "/pre" code, that have to be removed. Some code like this:
<pre class="brush:xml;toolbar:false;gutter:true;"><constructiongroups xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance"<br>
xsd:noNamespaceSchemaLocation="svgdescription.xsd"><br>
<constructiongroup><br>
<fza>5</fza><br>
<<a title="HST" href="1802.htm#o2441">hst</a>>10</hst><br>
<<a title="HT" href="1802.htm#o2442">ht</a>>30</ht><br>
In the meantime I found espresso to work with, but it doesn't satisfy my needs. I found the following regex:
(?:<pre .*\b>)*(?:<a[^>].*href="[0-9]+\.htm\#o[0-9]+"[^>]*>)(?:</a>)(?:</pre)
But it's not the end of the flagpole, but I don't know how to complete it. At the moment the regex is working partialy, but not as a whole.
Can someone help me, PLEASE?
Cheerio,
Heike
modified 2-Oct-12 6:52am.
|
|
|
|
|
Could you please close the <a>? It's messed up your post.
|
|
|
|
|
Done.
|
|
|
|
|
You don't seem to be allowing any content between the <a> and </a>. I think you need (.*) in there (it should be a replacement group because it's what you want to keep), and the matching to be done non-greedily.
Also, you don't allow for multiple <a> inside the same <pre>.
I don't really think regex is the right tool for this job, as there's lots of context and state-change in what you're trying to do. An XML based solution seems better to me. But something like
<pre[^>]*>(.*)(?:<a[^>]*>(.*)</a>(.*))*</pre>
... in non-greedy mode and writing all the found groups into the output might work.
|
|
|
|
|
Hi,
thanks for you answer. The content between "a" and "/a" has to stay, so my variant might be ok on this issue. But I never get the complete content of the "pre" tag. This is quite a problem.
What do you mean with "xml based solution"? How might xml help me on changing heaps of html-pages?
|
|
|
|
|
Sometimes it makes sense to split the problem up. Rather than attempting to grab the pre tags that contain a tags, why not grab the pre tags and then iterate over them - and do a second regex for the a tag. That way, you have the full context without worrying about backtracking.
|
|
|
|
|
My friend is making windows form and he needs to vlidate a username which will be a minimum of 4 characters and maximum of 15 characters long. It will also allow hyphens and underscores as well as dots in the middle, but not at the start and neither at the end of the username. There may be no more than one hyphen, one underscore and one dot in a row
Examples of disallowed usernames:.
-aquib
_aquib
.aquibxyz
aquib.
aquibxyz--qureshi
aquib__xyzqureshi
aquibqureshi-
aquib_qureshi-qureshi
aquib..qureshi
1236584 // not allow only numbers in username
aquib_ // means no symbols will be there at end
The username should not be only digits it should be either a mix of digits and alphabetical characters or it should be only alphabetic.
I hope this will be understood
I have got this regex:
^([a-zA-Z0-9](?(?!__|--)[a-zA-Z0-9_\-]){0,4}[a-zA-Z0-9])$
which is not usefull enough
|
|
|
|