user2593572
user2593572

Reputation: 11

AutoHotKey Script - String Splitting

I have a string that looks like this:

17/07/2013   TEXTT TEXR          1  Text                                 1234567            456.78     987654

I need to separate this so I only end up with 2 values (in this example it's 1234567 and 456.78). The rest is unneeded.

I tried using string split with %A_Space% but as the whole middle area between values is filled with spaces, it doesn't really work.

Anyone got an idea?

Upvotes: 1

Views: 1744

Answers (2)

Grey
Grey

Reputation: 339

src:="17/07/2013   TEXTT TEXR          1  Text                                "
. " 1234567            456.78     987654", pattern:="([\d\.]+)\s+([\d\.]+)"
RegexMatch(src, pattern, match)
MsgBox, 262144, % "result", % match1 "`n"match2

Upvotes: 1

bgmCoder
bgmCoder

Reputation: 6371

You should look at RegExMatch() and RegexReplace().

So, you will need to build a regex needle (I'm not an expert regexer, but this will work)

First, remove all of the string up to the end of "1 Text" since "1 Text" as you say, is constant. That will leave you with the three number values.

Something like this should find just the numbers you want:

needle:= "iO)1\s+Text"
partialstring := RegexMatch(completestring, needle, results)

lenOfFrontToRemove := results.pos() + results.len()
lastthreenumbers := substr(completestring, lenOfFrontToRemove, strlen(completestring) )
lastthreenumbers := trim(lastthreenumbers)
msgbox % lastthreenumbers

To explain the regex needle: - the i means case insensitive - the O stands for options - it lets us use results.pos and results.len - the \s means to look for whitespace; the + means to look for more than one if present.

Now you have just the last three numbers.

1234567            456.78     987654

But you get the idea, right? You should able to parse it from here.

Some hints: in a regex needle, use \d to find any digit, and the + to make it look for more than one in a row. If you want to find the period, use \.

Upvotes: 0

Related Questions