I have a autohotkeyscript with:
IfInString, pp_text, %A_Space%
{
pp_text := %pp_text%
}
So in case %pp_text% contains a space I want to add " at the beginning and end
Example:
pp_text = Hello World
should then become
pp_text = "Hello World"
How can I do this
You escape a quote by by putting another quote next to it and you concatenate with the concatenate operator ., but actually you can also just omit the operator 99% of the time.
Other things to fix in your script:
Get rid of that super deprecated legacy command and use InStr() instead.
And when you're in an expression, you refer to a variable by just typing its name. Those double % you're using are the legacy way of referring to a variable.
So it's correct in the legacy command, but not in the modern := assignment.
And also you can omit brackets on one-liner if-statements. But that's of course just going to be down personal preference.
Full script:
If (InStr(pp_text, A_Space))
pp_text := """" pp_text """"
Four quotes since the the outer two specify that we're typing a string.
Variable names in an expression are not enclosed in percent signs.
Consequently, literal strings must be enclosed in double quotes to
distinguish them from variables.
To include an actual quote-character
inside a literal string, specify two consecutive quotes as shown twice
in this example: "She said, ""An apple a day.""".
pp_text := "Hello World"
If InStr(pp_text, " ")
pp_text := """Hello World"""
MsgBox % pp_text
EDIT:
To use the name of the variable (not its literal text) in the output expression, you need four quotes, as the user 0x464e explained.
pp_text := "Hello World"
If InStr(pp_text, " ")
pp_text := """" pp_text """"
MsgBox % pp_text
Related
I am trying to parse all the files and verify if any of the file content has strings TESTDIR or TEST_DIR
Files contents might look something like:-
TESTDIR = foo
include $(TESTDIR)/chop.mk
...
TEST_DIR := goldimage
MAKE_TESTDIR = var_make
NEW_TEST_DIR = tesing_var
Actually I am only interested in TESTDIR ,$(TESTDIR),TEST_DIR but in my case last two lines should be ignored. I am new to perl , Can anyone help me out with re-rex.
/\bTEST_?DIR\b/
\b means a "word boundary", i.e. the place between a word character and a non-word character. "Word" here has the Perl meaning: it contains characters, numbers, and underscores.
_? means "nothing or an underscore"
Look at "characterset".
Only (space) surrounding allowed:
/^(.* )?TEST_?DIR /
^ beginning of the line
(.* )? There may be some content .* but if, its must be followed by a space
at the and says that a whitespace must be there. Otherwise use ( .*)?$ at the end.
One of a given characterset is allowed:
Should the be other characters then a space be possible you can use a character class []:
/^(.*[ \t(])?TEST_?DIR[) :=]/
(.*[ \t(])? in front of TEST_?DIR may be a (space) or a \t (tab) or ( or nothing if the line starts with itself.
afterwards there must be one of (space) or : or = or ). Followd by anything (to "anything" belongs the "=" of ":=" ...).
One of a given group is allowed:
So you need groups within () each possible group in there devided by a |:
/^(.*( |\t))?TEST_?DIR( | := | = )/
In this case, at the beginning is no change to [ \t] because each group holds only one character and \t.
At the end, there must be (single space) or := (':=' surrounded by spaces) or = ('=' surrounded by spaces), following by anything...
You can use any combination...
/^(.*[ \t(])?TEST_?DIR([) =:]| :=| =|)/
Test it on Debuggex.com. (Use 'PCRE')
I know that you can assign a multi-line string to a variable like this:
MyVar =
(
this
is
a
string with multiple
lines
)
But is there a way to assign the above string to an object property? I tried doing it like this but I received an error:
Array := {}
Array["key"] =
(
this
is
a
string with multiple
lines
)
The error says:
The following variable name contains an illegal character
"this
is
a
string"
I just want to be able to open my script in a text editor and copy and paste multiple-line strings directly into the editor as properties of objects.
You have to use the proper assignment operator := with Objects, likewise your text needs be enclosed by Quotes.
Try:
obj := {}
obj["key"] :=
(
"this
is
a
string with multiple
lines"
)
MsgBox % obj["key"]
Or you can do this below:
x =
(
this
is
a
string with multiple
lines
)
obj["key"] := x
MsgBox % obj["key"]
You can also build a multi-line object like so:
obj := {"key":
(
"this
is
a
string with multiple
lines"
)}
MsgBox % obj["key"]
Using a raw multiline string assignment like the following it tends to defeat any indenting you may have cultivated in your script.
str := {"Lines":
(
"first
second
third"
)}
Although that will work. If you're looking to preserve your code indenting then you can create a multiline string by delimiting the lines with `n like this:
str := {"Lines": "first`nSecond`nThird"}
I am using Autohotkey.
I have a string that looks like this S523.WW.E.SIMA. I want to remove the last few characters of the string after the dot (including the dot itself). So, after the removal, the string will look like S523.WW.E.
This may look like a simple question but I just cannot figure out using the available string functions in Autohotkey. How can this be done using Autohotkey? Thank you very much.
Example 1 (last index of)
string := "S523.WW.E.SIMA"
LastDotPos := InStr(string,".",0,0) ; get position of last occurrence of "."
result := SubStr(string,1,LastDotPos-1) ; get substring from start to last dot
MsgBox %result% ; display result
See InStr
See SubStr
Example 2 (StrSplit)
; Split it into the dot-separated parts,
; then join them again excluding the last part
parts := StrSplit(string, ".")
result := ""
Loop % parts.MaxIndex() - 1
{
if(StrLen(result)) {
result .= "."
}
result .= parts[A_Index]
}
Example 3 (RegExMatch)
; Extract everything up until the last dot
RegExMatch(string, "(.*)\.", result)
msgbox % result1
Example 4 (RegExReplace)
; RegExReplace to remove everything, starting with the last dot
result := RegExReplace(string, "\.[^\.]+$", "")
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?
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
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 \.
I have stdout feeding to a variable when I use literal filepaths in my comspec line, but why can I not use a variable in place of the filename I want to be analyzed?
Note that mediainfo already (successfully) uses the % symbol in its own arguments. So how do I make comspec treat %LongPath% as a real variable?
I already tried adding an extra pair of quotes around %Longpath% as well, but no luck.
Loop %0%
{
Path := %A_Index%
Loop %Path%, 1
LongPath = %A_LoopFileLongPath%
SplitPath LongPath, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
objShell := ComObjCreate("WScript.Shell")
objExec := objShell.Exec(comspec " /c C:\MediaInfo.exe --Inform=Video;%FrameRate% %LongPath%")
framerate := ""
while, !objExec.StdOut.AtEndOfStream
framerate := objExec.StdOut.ReadAll()
msgbox %framerate%
}
Thanks for some expertise.
The key was the enclose the appname and arguments in quotes, (and make sure to leave an extra space before the last quote) and then not to include your final variable in the quotes.