Jump to content

Extract mid-string data


czardas
 Share

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
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. ;)
Link to comment
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 ;)

Link to comment
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!
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...