Regular Expressions
|
|
 |

|
I'm piecing toget code formv various places becuase it all looks like greek to me. I'm fairly frustrated at this point. This shouldn't be this difficult.
Anyway, here's my code:
if (Line.Contains(WordToFind))
{
Regex r = new Regex(
"(?:(?'Key'\\S+): (?'Value'.*?))",
RegexOptions.RightToLeft
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
Match m = r.Match(WordToFind);
if (m.Success)
{
}
}
I get no matches for the word "Model".
Also, once I get a match, how do I get the Key/Value data???
If it's not broken, fix it until it is
|
|
|
|

|
Why did you edit your response? The code I see in the email notification I was sent for your reply to my message looks more correct than the code I now see.
For one, you're going to need a for loop to iterate over the result of Matches (not Match). Match finds a single result, and Matches finds all results.
For two, r.Match(WordToFind) is searching "WordToFind" rather than "Line".
Kevin Marois wrote: once I get a match, how do I get the Key/Value data
You can get groups based on the name. Something like this (I don't have my compiler open, so it may vary slightly):
String key = m.Groups["Key"].Value;
String value = m.Groups["Value"].Value;
Kevin Marois wrote: RegexOptions.RightToLeft
This may drastically change the functionality, but I'm not exactly sure what it does, so you may want to Google this.
Kevin Marois wrote: I'm fairly frustrated at this point. This shouldn't be this difficult.
Regular expressions are complicated, but powerful. You'll get used to them over time. From what others say, Expresso is a good tool to learn regular expressions, though I use a custom tool I built for myself.
|
|
|
|

|
pretty new to regex. this is a great article.
can someone tell me whats wrong with my expression? I'm using this from C#. I'm getting the response from a blog in a malformed xml format. Need to extract entries out of it. In a simple format inpu is similar to the following.
string input = @"<entry><id>tag:myblog.com try</entry><entry><id>tag:myblog.com tryagain</entry><entry><id>tag:myblog.com hello </enty>";
I need to identify the number of entries, and then processing each of them.
Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*</entry>");
MatchCollection blogEntries = blogsRegEx.Matches(input);
I always get just 1 entry. It matches the whole thing instead of matching multiple strings in the pattern <entry<id>tag:myblog....</entry>.
Can someone help what am I missing here? do i need to use subexpressions here?
modified 29 Apr '12 - 19:28.
|
|
|
|

|
Not sure about the regex (It makes my brain hurt), but you can use linq instead
string findText = @"<entry><id>tag:myblog";
int entriesCount = blogsText.Count(t => t.equals(findText));
Have not tested it but it should give you the correct results by just tweaking your findText variable.
|
|
|
|

|
By default, regex matching is greedy. That is, wildcards will match the longest possible chunk of input, so you need to make your .* non-greedy. You do that by putting a ? after it, so your line becomes Regex blogsRegEx = new Regex(@"<entry><id>tag:myblog.*?</entry>");
If you are going to do ANYTHING nontrivial with regexes, get a copy of Expresso. (See our Free Tools forum for details.)
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994.
|
|
|
|

|
I try to get the date number format from a date format string. The date number format is - if present - either a single d or a double d. But the whole string can contain a place holder for the dayname also - ddd or dddd. How can I get the d or dd, if present?
I tried [^d](?<pattern>d{1,2})[^d] which works well with the standard German format (dddd, d. MMMM yyyy). But that fails when the daynumber comes first (say: d. dddd MMMM yyyy; here, ^(?<pattern>d{1,2})[^d] does the trick), and it fails when the daynumber comes last.
Actually I need something like a "either start of the string or not a d", i.e. ^|[^d] - but that does not work. How can that be solved?
|
|
|
|

|
Solved it with @"\b(?d{1,2})\b"
|
|
|
|

|
Given the name of an SQL table, I am looking to add quotes as necessary (with an loose definition of "necessary"). With SQL Server, the parts of the name should be wrapped in brackets ([ and ]), MySQL uses backticks (`). So, for example:
database.schema.table
database.schema.[table]
database.[schema].table
[database].[schema].table
should all be transformed to:
[database].[schema].[table]
What I have working; is capturing Wrapped and Unwrapped sections separately, wrapping the Unwrapped sections, and joining the sections back together. But it occurred to me that if I could Match only the Unwrapped sections, I could use Replace.
However, I have so far been unsuccessful in my attempts (otherwise I wouldn't be posting). Does anyone out there have an idea of how to do this? I'm thinking it may involve Balancing Groups, but I've never used them before so I'm finding them confusing.
This is not urgent.
Edit: I must have been over-thinking it . What I have now is (?<=^|\.)[^\[\]\.]+(?=$|\.)
modified 24 Jan '12 - 11:00.
|
|
|
|

|
Are you looking for this?
string sql = @"database.schema.table
database.schema.[table]
database.[schema].table
[database].[schema].table
should all be transformed to:
[database].[schema].[table]
";
string pattern = @"(?:\[?(\w+)\]?)?\.\[?(\w+)\]?";
Func<Match, string> replace = m =>
(m.Groups[1].Success ? "[" + m.Groups[1].Value + "]" : "") + ".[" + m.Groups[2].Value + "]";
Console.WriteLine("{0}", Regex.Replace(sql, pattern, m=>replace(m)));
|
|
|
|

|
Does that work for names that contain SPACEs? my database.my schema.my table
And Excel worksheet names that include a dollar sign ($) at the end?
(I realize those were not listed in the original spec.)
|
|
|
|
 |
|
|
General
News
Suggestion
Question
Bug
Answer
Joke
Rant
Admin