|
I thought that $ sign is supposed to indicate the end of a match. But I have seen patterns like (?<firstname>.*$) in some regular expressions samples. What could it mean in such a pattern?
Also I read in a book that the pattern (?<1>a)(?<1>\1b)* matches aababb with the capturing pattern (a)(ab)(abb).
Wasn't it better to use <2> for the second group? Why do we have two (?<1>) and why is there a back reference right after <1>? I mean \1.
Can someone please describe these regex samples? And please if this is not the right forum to ask this type of questions, let me know.
|
|
|
|
|
The dollar sign in your first example IS the last character in the pattern (it just happens to be in parenthases). The other use of $ is in replacement patterns. I'm not sure what happens if $ appears prior to the end of a regex pattern, but it either matches nothing, matches newline character, or matches $ character.
I don't have any info for you on the second pattern. The place I usually go to find Regex info is by navigating to "ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxfund/html/930653a6-95d2-4697-9d5a-52d11bb6fd4c.htm" in the Visual Studio Help->Search window.
Jeff
|
|
|
|
|
First off, this[^] page is a great reference.
As for you questions, the $ is described under the "Atomic Zero-Width Assertions" section as "Specifies that the match must occur at the end of the string, before \n at the end of the string, or at the end of the line". Whether it matches at the end of the string or before a newline is based on the RegexOptions[^]. MSDN decribes the Multiline option as "Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.".
If you need to match a literal $ then you need to preceed it with a backslash (\) or include it in a character class (e.g. [$]).
Technically, (?.*$) is not valid. You may have meant (?:.*$), which is a non-capturing grouping construct (the (? ) part). See the "Grouping Constructs" from the link above. Basically, this tells the regex engine that you do not want to capture the characters matched by the .*$ part into a separate group. They will still be included in the overall match though.
For your second question, this is a form of recursion really. Hopefully, this break down will explain it:
(the | character indicates the current position of the regex engine)
1. |aababb
The (?<1>a) portion will match "a" from the input and save it in a group called "1".
2. a|ababb
The (?<1>\1b) portion is really (?<1>ab) at this point because group "1" has the value "a", so it will match "ab" from the input and overwrite the value in group "1" with "ab".
3. aab|abb
Since the (?<1>\1b) has the *, which means zero or more matches, then we perform this portion again. The (?<1>\1b) portion is really (?<1>abb) at this point because group "1" has the value "ab", so it will match "abb" from the input and overwrite the value in group "1" with "abb".
4. aababb|
We are at the end of our string so we are done. Now the full string matched would be "aababb". The value of the group "1" would be "abb", eventhough it previously held the values "a" and "ab".
If we had used <2> in the second construct, then step 1 would work the same, but step 2 and beyond would work like this:
2 (alt). a|ababb
The (?<2>\1b) portion is really (?<2>ab) at this point because group "1" has the value "a", so it will match "ab" from the input and save the value in a group called "2".
3 (alt). aab|abb
Since the (?<2>\1b) has the *, which means zero or more matches, then we perform this portion again. The (?<2>\1b) portion is really (?<2>ab) at this point because group "1" has the value "a", so it will match "ab" from the input and overwrite the value in group "2" with "ab".
4 (alt). aabab|b
At this point we can't match any more characters so we are done. Now the full string matched would be "aabab" (one less char than above). The value of the group "1" would be "a" and the value of the group "2" would be "ab".
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Wow, this is one of the most helpful responses I have ever seen. You are definitely getting a 5 for this one.
Jeff
|
|
|
|
|
Thanks! I've always loved regex's
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Thank you TJoe. It was a great answer to my question. Completely understood now. You are the man!
|
|
|
|
|
the problem is in form load event i kept the filling of the combobox named Gender from SQL tables
it is giving error in the following line
this.cGender.valuemember = "GenID";
(the value not found blah blah)
why it is doing so
sas
|
|
|
|
|
sajid.salim.khan wrote: in form load event i kept the filling of the combobox named Gender from SQL tables
Is there some reason you are not using data binding?
|
|
|
|
|
sajid.salim.khan wrote: the value not found blah blah
Hmm, interesting error message. Not seen that one before .
sajid.salim.khan wrote: this.cGender.valuemember = "GenID";
Does the data source you are binding to contain a field called 'GenID'?
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
the situation is that i have a table Employee info having attributes
EmpId
EmpName
GenID
the gender attribute is bieng filled by another table having 2 attribuites GenID and gender
i.e GenID is a foriegn key in the Employee info table
now when filling the table from a winform i loaded the data in the combo box of gender in form load event
but the value member is giving error bcoz i have to select the field from gender and must be saved in the Employee info table
sas
|
|
|
|
|
Hello everyone.
I'm still in the process of learning C# (but not programming) so bear with me if this is something really trivial and a "duh" question
How can I pass variables to a command button? For example, I'm working on an inventory/task system for work. I look up a laptop that has an asset tag of 740 for example, and on that Windows form I have a button that allows me to look up all tasks associated with that asset tag. My SQL query would be like "select * from tasks where asset_tag = '740'" but I don't know how to pass that SQL query the "740" piece. Hopefully this all makes sense and I haven't sounded like an idiot
Thanks guys!
Chip
|
|
|
|
|
The asset tag value can be from a textbox or a dropdownlist, where a user can enter or choose a value, so what you need to do is to form the query:
"select * from tasks where asset_tag = '" + txtAssetTag.Text + "'"
where txtAssetTag is the said textbox.
By the way you should use parameterized query to avoid injection attack.
|
|
|
|
|
Thanks darkelv.
Yeah I'm going to go with the parameterized query, just through up a quick example of what I was trying to ask .
|
|
|
|
|
darkelv wrote: "select * from tasks where asset_tag = '" + txtAssetTag.Text + "'"
No! NO! NO! NO! NO! NO!!!!
Never advise anyone in any way which would obviously lead to a SQL Injection Attack.
Please read SQL Injection Attacks and Tips on How To Prevent Them[^].
And yes, I know you suggested to use paramerised queries to avoid attack, but your sample should have used that from the off.
|
|
|
|
|
Which is why I suggested him to use the parameter thingie in the end
|
|
|
|
|
All Controls have a Tag property which lets you store custom data.
|
|
|
|
|
Hi
I have a problem while using the Filesystem watcher component in conjunction with a Windows form application.
I'm trying to do the following:
The form should show the content based on af file (which can be modified from another application!) and this should be done dynamically while the form runs...
So what I've done is to add a standard form to the app and then build the form contents dynamically based on the information contained in a file. The form contents is a panel and on that some labels and picture boxes...
So I would like to register a filesystem event monitor to detect when the file changes and re-create the Panels contents dynamically...now the problem occus...!
When trying to update the panel I get an error saying that:
Cross-thread operation not valid : Control 'pnlUpdates' accessed from a thread other than the thread it was created on.
The line of code that causes this error is:
pnlUpdates.Controls.Add(lblTest);
I suppose it is because the filesystem watcher is running in another thread and therefore this is not allowed?? I tried to use a backgroundworker to allow the update but this fails if I start the process from the OnChanged event of the Filesystem eventwatcher (same thread error as above)...
Anyone who knows how this can be done?
Kind regards
Thomas
|
|
|
|
|
Must say I'm chuffed - I figured out the problem by reading just the subject.
I had the same issue in a different context the other day; the problem is that Winforms controls are not thread safe. I'm not at work right now, so I don't have the code, but you will have to use a delegate in your form class to point to the method that actually does the work. In your event handler, you will have to invoke the delegate like
this.Invoke(new YourDelegate(TheMethodThatUpdatesTheControls)); The copy of MSDN on my colleague's machine was very helpful with this. If you're still having problems, just yell and I will give you the code when I get back to work tomorrow.
Cheers,
विक्रम
And sleep will come, it comes to us all
And some will fade and some will fall
|
|
|
|
|
Hi
Thanks a lot for the help. Used the invoke method and it works like a charm!
Kind regards
Thomas
|
|
|
|
|
tsiv wrote: Used the invoke method and it works like a charm!
It's not a charm. The behavior and solution are well documented. If you want to be a software developer rather than a snake charmer I strongly suggest you start reading the documentation.
|
|
|
|
|
Don't you have something better to do than drop stupid comments on this forum!?
Fool.
I had a problem - Vikram was kind enough to point me in the direction of a solution - why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that...
Thomas
|
|
|
|
|
tsiv wrote: why do you even bother to give feedback if the feedback isn't worth anything...?? I don't understand that...
Of course you don't get it because you don't comprehend the value of reading the documentation. I can point that out to people like you but I can't make you understand it.
There is an old proverb, once again I can recite it but I can't make you understand it:
“Give a man a fish and you feed him for a day, teach a man to fish, and you feed him for a lifetime.”
|
|
|
|
|
|
You might want to read this article. Hopefully it will clear up a lot of things: http://www.codeproject.com/csharp/begininvoke.asp
|
|
|
|
|
Hello, I'm writing a ALT+TAB-ish app that pops up due to a hotkey. It currently displays a list of currently running applications (using EnumWindows & GetWindowText) as a row in a DataGridView and I've created a event handler to run when a row is double clicked on.
I'm hoping to use SetForeGroundWindow from User32.dll to bring the selected app to the front. This requires a pointer (IntPtr) to be passed, which I have available when populating my grid, but I can't see to keep around in IntPtr format. When I assign the IntPtr to a cell value, and then try to recast it as a pointer again, it bombs:
_hWnd = (IntPtr)dgvTaskList.SelectedRows[0].Cells[2].Value;
SetForegroundWindow(_hWnd);
I need to either
A) store a raw IntPtr in a cell (not as a string)
B) be able to convert the string back to a ptr
C) something else entirely that I'm not thinking of
I'm open to any help or suggestions from my code project peers...
Thanks!
-- modified at 12:13 Tuesday 27th November, 2007
|
|
|
|