czardas Posted December 31, 2009 Share Posted December 31, 2009 (edited) I'm not quite sure how to word this. I want to grab everything between [pattern " and "] within a string: as shown in the code.Dim $tags, $tagTrimLeft, $tagTrimRight $tags = '[Tag1 "I am not interested in this string"]' & @CRLF & _ '[Tag2 "Nor this one"]' & @CRLF & _ '[Pattern "The string I am interested in."]' & @CRLF & _ ; Grab everything between double quotes on this line. '[Tag4+ "Any number of trailing tags"]' & @CRLF & @CRLF ; Instead of the following code, can I simply use a regular expression? $tagTrimLeft = StringSplit($tags, '[Pattern "', 1) ; Separate Pattern from any preceeding tags. If StringLeft($tags, 10) == '[Pattern "' Then ; Test if the pattern appears in the first tag. $tagTrimRight = StringSplit($tagTrimLeft[1], '"]', 1) ; Separate any trailing tags. Else $tagTrimRight = StringSplit($tagTrimLeft[2], '"]', 1) ; Likewise. EndIf MsgBox(0, "Return", $tagTrimRight[1])I guess there must be a simpler way to do this with regular expressions. Does anyone know? Because I have tried, but I can't figure it out. Edited December 31, 2009 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted December 31, 2009 Share Posted December 31, 2009 (edited) I guess there must be a way to do this with regular expressions. Does anyone know? Because I have tried, but can't figure it out.Open the helpfile and look at _StringBetween, is that what you want? It can do both string and regexp so you can open its source and see how it does.Tip: Press CTRL+J to jump to the function declaration when you have it selected (like F1 or ALT+I). Edited December 31, 2009 by AdmiralAlkex .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
czardas Posted December 31, 2009 Author Share Posted December 31, 2009 Open the helpfile and look at _StringBetween, is that what you want? It can do both string and regexp so you can open its source and see how it does.Tip: Press CTRL+J to jump to the function declaration when you have it selected (like F1 or ALT+I).I knew there must be a way. That sounds like just what I need. Thanks a lot. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Mison Posted December 31, 2009 Share Posted December 31, 2009 Regex way: Dim $tags, $tagTrimLeft, $tagTrimRight $tags = '[Tag1 "I am not interested in this string"]' & @CRLF & _ '[Tag2 "Nor this one"]' & @CRLF & _ '[Pattern "The string I am interested in."]' & @CRLF & _ ; Grab everything between double quotes on this line. '[Tag4+ "Any number of trailing tags"]' & @CRLF & @CRLF $tagRegex = StringRegExpReplace($tags, '[\s\S]+Pattern "([^"]+)"[\s\S]+', '\1') MsgBox(0, "Return", $tagRegex) I never use _StringBetween, but I might want to take a look at it later since a lot of people have suggested it. Hi ;) Link to comment Share on other sites More sharing options...
czardas Posted December 31, 2009 Author Share Posted December 31, 2009 Regex way: Dim $tags, $tagTrimLeft, $tagTrimRight $tags = '[Tag1 "I am not interested in this string"]' & @CRLF & _ '[Tag2 "Nor this one"]' & @CRLF & _ '[Pattern "The string I am interested in."]' & @CRLF & _ ; Grab everything between double quotes on this line. '[Tag4+ "Any number of trailing tags"]' & @CRLF & @CRLF $tagRegex = StringRegExpReplace($tags, '[\s\S]+Pattern "([^"]+)"[\s\S]+', '\1') MsgBox(0, "Return", $tagRegex) Neat! I like the back-reference part: \1. Very useful and interesting, thanks! operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Mison Posted December 31, 2009 Share Posted December 31, 2009 No problem. I've got that(the backreference thing) from here. Interesting, isn't it? Hi ;) Link to comment Share on other sites More sharing options...
czardas Posted December 31, 2009 Author Share Posted December 31, 2009 No problem. I've got that(the backreference thing) from here. Interesting, isn't it? I only recently started using regular expressions. It opens up a whole new world. The syntax is pretty crazy, but it's definately worth getting used to. operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now