Sign in to follow this  
Followers 0
czardas

Extract mid-string data

7 posts in this topic

#1 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by AdmiralAlkex

Share this post


Link to post
Share on other sites

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. ;)

Share this post


Link to post
Share on other sites

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 ;)

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

No problem. I've got that(the backreference thing) from here. Interesting, isn't it? ;)


Hi ;)

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0