|
What I provided returns everything between the ]]] and the next [ or the end of the line.
|
|
|
|
|
I think (\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$)) is enough for both of ur requirements, if i understood it clearly
something like ...
Regex regex = new Regex(@"(\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$))");
Match regexmatch = regex.Match(@"[[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3");
regexmatch.Groups["MyProperty3"].Value
It'd work for values like value, value2 and value 3 as well.
Mark as answer if it answers ur question
modified on Friday, April 9, 2010 3:18 PM
|
|
|
|
|
I prefer a more generalized technique.
|
|
|
|
|
The property name is looks like this [[[MyProperty2]]] but does not end with <MyProperty2>. So the property/value item shows up as [[[MyProperty2]]] Value2. So if there are multiple properties it will look like this: [[[MyProperty1]]] Value1 [[[MyProperty2]]] Value2 [[[MyProperty3]]] Value3.
Having said that, the pattern provided did not work because I think it is looking for <MyProperty2> end tag. One other thing I want to point out is that there can be only one property which can or cannot have a space after it.
My requirements:
1) Get the value of a property if only one propery is listed ([[[MyProperty1]]] value1)
2) Get the value of a property if there are more than one properies listed ([[[MyProperty1]]] value1 [[[MyProperty2]]] value2 [[[MyProperty3]]] value3
3) In the pattern, I want to pass in the property name and get back the value
4) Use VBScript.RegexExp object (which I dont think will make a difference when it comes to pattern)
Thanks so much for assisting me with this.
|
|
|
|
|
I understand <myproperty2> is not in string of properties. Its a Named capture group literal in regex used to store matched strings under that name provided inside <>
This solution does worked with
MyProperty1, MyProperty2 and MyProperty3.
Try it urself !!!
Note: To get value of property, replace #### from
(\[\[\[####\]\]\][\s{1}])(?<####>.*?(?=\s{1}\[|$)) to that name
Example
To get MyProperty3
use regex (\[\[\[MyProperty3\]\]\][\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$))
Search for Named capture groups to understand use of <> in regex.
Edit: I have updated it lil bit to take care of end of line as well.
|
|
|
|
|
I wrote a pattern that works as well thanks to you and everyone else that helped me out. THANKS!
Here is my pattern in case you want to see it (and let me know if im missing something):
\[{3}MyProperty1\]{3} ([^ ]+)
|
|
|
|
|
Great !!!
Its always better to use Named capturing groups in complicated regex patterns. But this one is ok
BTW what about values with spaces ?
|
|
|
|
|
DOH!!! Values with spaces dont work.... how do I resolve that>
|
|
|
|
|
Dont worry my solution still works !!!
I guess u don;t need name captured groups, so I have removed it n here is new one
(\[{3}MyProperty4\]{3}[\s{1}])(.*?(?=\s{1}\[|$))
Mark as anwser !!!!
|
|
|
|
|
Works like a charm!!!
Any way I can just have the value returned?
|
|
|
|
|
Now time to callback my initial post.
Like I said use Named capturing groups. Using this you can just return the value of provided propertyname
Regex regex = new Regex(@"(\[{3}MyProperty3\]{3}[\s{1}])(?<MyProperty3>.*?(?=\s{1}\[|$))");
Match regexmatch = regex.Match(@"[[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3");
string value = regexmatch.Groups["MyProperty3"].Value
Above code will return value of property MyProperty3
|
|
|
|
|
I would LOVE an explanation on the logic behind this...
(\[{3}Property2\]{3}[\s{1}])(.*?(?=\s{1}\[|$))
The only part I understand is (\[{3}Property2\]{3}[\s{1}])
|
|
|
|
|
Yeah!!! u ought to know everything thats going around on earth
Google will take you to right place if you search for positive lookahead in regex
|
|
|
|
|
I prefer the technique I showed.
You then enumerate the Matches and fill a Dictionary with the Names and Values.
Your requirements 1 -- 3 are then satisfied by the Dictionary and you only have to execute the RegEx once per group of properties.
Fayu wrote: Use VBScript
Then why post it in the C# forum?
Or maybe you should give more information on the bigger picture of what you are trying to accomplish.
|
|
|
|
|
Your solution will also give same result, but it will return all properties.
As Fayu asked on his requirement, it wasnt he is looking for.
Cheers !!!
|
|
|
|
|
Navnath R. Kale wrote: it wasnt he is looking for
It should be.
|
|
|
|
|
Fayu wrote: I wish I can use the CLR but the requirements I have prevent me from using the CLR.
::Throws up hands::
|
|
|
|
|
Yea but I am not using the clr. The pattern he provided worked with all requirement, including not using CLR. I used his pattern using VBScript.RegexExp and it worked wonders.
Thanks for your help as well.
|
|
|
|
|
Mine works with perl:
perl -e "print \"[[[MyProperty1]]] Value [[[MyProperty2]]] value2 [[[MyProperty3]]] value 3\" =~ m/(?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*))/g"
%stuff = $ARGV[0] =~ m/(?:\[\[\[(?'Name'.*?)\]\]\](?'Value'[^\[]*))/g ;
foreach $id ( sort ( keys ( %stuff ) ) )
{
print ( "$id = $stuff{$id}\n" ) ;
}
modified on Saturday, April 10, 2010 12:33 AM
|
|
|
|
|
Well i guess, all this confusion is because question is in wrong section.
Perl supports Named capture groups but client scripts don't.
|
|
|
|
|
Navnath R. Kale wrote: Named capture groups
You don't need to name them. The important thing is to have the engine return all the matches and enumerate them.
|
|
|
|
|
!!! Now the thread becomes the session.
Actually (?'Name') and (?'Value') is equal to (?<Name>) and (?<Value>) and is nothing but Named capturing group.
.Net supports it both way.
|
|
|
|
|
Yes. I prefer the apostrophes -- at least I don't need to escape them when I post them.
|
|
|
|
|
Yeah On first post I was like " WTF happened to my pattern, it was correct when I typed"
|
|
|
|
|
can any one send me sample application on hottspot
saradhi
|
|
|
|