Ben! Its no secret I've always been not too keen on regex, but this is really killing me. I've been reading your regex posts trying to figure how to parse a string just like this: [event=action][id=longuuid][a=b][c=D] and so forth. After a good hour of eye strain and plucking at my keyboard, I thought it might be wise to ask for your help. The best I could come up with was: strData = str.split('\[?\[*\[?\]') which left a much annoying [ at the beginning of each index. At one point I was able to get that [ appear on just the First index, but... thats useless too. Hope you can help, take it easy!
You are on the right track - regular expressions are definitely something that we can use in this situation. The trick here is to see how we can break down the regular expression pattern. As you can see, when we look at your sample data string and abstract it out, what is it? It's really a series of values that match this general pattern:
Now that we have that, what can our name consist of? For the sake of the demo, I am going to say it can consist of any "word" character:
Likewise, I will say that the value will contain every character until the closing bracket:
Now, let's combine all of that into a single regular expression:
When we run this code, we get the output:
event : action
id : longuuid
a : b
c : D
The data string is successfully parsed into a collection of name-value pairs and the output to the screen.
I hope this helps.
Want to use code from this post? Check out the license.
I didn't know you could do that with string.replace(). I always thought- and read- that the second argument had to be a string.
Yeah, it's an awesome feature!
If I for instance want to remove dashes from a UUID, I do:
So, myString.replace('-',''); will replace the first - with nothing, but by simply adding the slashes and the 'g' you emulate the 'All' attribute in CF.
"g": global replace. As you said, if you leave this out, the replace() method will only execute the first replace.
"i": case-insensitive matching.
"m": multi-line matching.
a bit off-topic (using your site search (several different searches) didn't deliver the post describing your site-changes as regards to the commenting system), but how does this new commenting system work for you? I cannot see that your number of comments or points increase when you add a comment ;-) Or don't the rules apply to you as a site-owner?
Oh, and antoher thing, how can I get my picture next to my comment? Where do you get the picture from?
The comment data is not displayed on a per-comment basis, although that would be a cool feature. All comments show the current user data, which is why you are not seeing any increment.
The comment pictures are being pulled from: http://en.gravatar.com.
And how are the points counted? I thought 2 points per comment (I had 16 comments and 32 points) but after my 17th comment I suddenly had 44 points ;-) Or is that giving away too much? Maybe a (another) sick (e.g. good!) RegEx you've written?
The points per comment is a bit calculated and a bit random (for fun).