murilo.xd
murilo.xd

Reputation: 69

ahk - Get text after caracter (space)

I'm new to Autohotkeys. I'm trying to remove all the text up to the first space on each line, getting everything else.

example:

txt1=something
txt2=other thing

var.="-1" " " txt1 " " txt2 "`n"
var.="2" " " txt1 " " txt2 "`n"
var.="4" " " txt1 " " txt2 "`n"
;; more add ...

FinalVar:=var
;...
msgbox % FinalVar
RETURN

Current output:

-1 something other thing
2 something other thing
4 something other thing

how I wish (all lines of FinalVar whitout need Loop):

something other thing
something other thing
something other thing

In bash i could use something like SED

Is there a fast way to do the same thing in ahk?

Thanks to your atention. Sorry my english!

Upvotes: 1

Views: 1856

Answers (1)

Yane
Yane

Reputation: 837

You can use a combination of the InStr command

InStr()
Searches for a given occurrence of a string, from the left or the right.
FoundPos := InStr(Haystack, Needle , CaseSensitive := false, StartingPos := 1, Occurrence := 1)

and SubStr command.

SubStr()
Retrieves one or more characters from the specified position in a string.
NewStr := SubStr(String, StartingPos , Length)

With InStr you find the position of the first space in var.

With SubStr you extract everything after that position to the end of the string like this:

StartingPos := InStr(var, " ")
var := SubStr(var, StartingPos + 1)

Note the + 1, it is there because you need to start extracting the text 1 position after the space, otherwise the space will be the first character in the extracted text.


To replace the leading text in all lines you can use RegExReplace

RegExReplace()
Replaces occurrences of a pattern (regular expression) inside a string.

NewStr := RegExReplace(Haystack, NeedleRegEx , Replacement := "", OutputVarCount := "", Limit := -1, StartingPosition := 1)

FinalVar := RegExReplace(var, "m`a)^(.*? )?(.*)$", "$2")

m`a)are RegEx options, ^(.*? )?(.*)$ is the actual search pattern.

m Multiline. Views Haystack as a collection of individual lines (if it contains newlines) rather than as a single continuous line.

`a: `a recognizes any type of newline, namely `r, `n, `r`n, `v/VT/vertical tab/chr(0xB), `f/FF/formfeed/chr(0xC), and NEL/next-line/chr(0x85).

Upvotes: 1

Related Questions