I need help with preg_match in lua, I have tried some different ways.
I tried with string.match and string.find, but I can't write the pattern correctly.
I know this way in php, but need similar way in lua
$s = "/testas [51]";
preg_match("#/testas [(.*)]#", $s, $out);
print_r($out);
I want to get an integer in [ ].
To get the integer inside [ ], try this code:
print(string.match(s,"/testas%s+%[(%d+)%]"))
The pattern comes directly from the specification of the task. We just need to escape the brackets.
Related
please tell me an analogue of a C ++ function but in lua
replaceAll("[^\\dA-Za-z]", "")
You can use Lua's patterns for that (which are not to be confused with regular expressions), in combination with string.gsub.
In your case, it's probably something like
local sanitized = raw:gsub("[^%dA-Za-z]", "")
(I don't have access to a Lua REPL at the moment, so this code is untested, but the links to the documentation should help you if in doubt)
I'm looking for a way search in a string for a very specific set of characters: "(),:;<>#[\]
specialChar = str:find("[\"][%(][%)][,][:][;][<][>][#][%[][%]][\\]")
I'm thinking that there will be no pattern to satisfy my need because of the Limitations of Lua patterns.
I have read the Lua Manual pattern matching section pretty thoroughly but still can't seem to figure it out.
Does anyone know a way I can identify if a given string contains any of these characters?
Note, I do not need to know anything about which character or where in the string it is, if that helps.
To check if a string contains ", (, ), ,, :, ;, <, >, #, [, \ or ] you may use
function ContainsSpecialChar(input)
return string.find(input, "[\"(),:;<>#[\\%]]")
end
See the Lua demo
I am attempting to define a syntax to parse data definitions in COBOL and had a particular definition for picture clauses like this:
syntax PictureClause = pic: "PIC" PictureStringType PictureStringLen ("VALUE"|"VALUES") ValueSpec
My matching ADT for this syntax was as so:
data PictureClause = pic(str pictype, PictureStringLen plen, str valuespec);
However, I noticed that it seems as if the implode function was attempting to match the parenthesized statement to the second str parameter, instead of ignoring it like the "PIC" string literal. However, this syntax definition worked as expected:
syntax PictureClause = pic: "PIC" PictureStringType PictureStringLen "VALUE" ValueSpec
|pic: "PIC" PictureStringType PictureStringLen "VALUES" ValueSpec;
As the title states, how can I define alternatives in a single statement for literals that I do not want in my ADT in a syntax definition? I can see that alternatives are possible, but I'm wondering if there is a more concise way of defining it, in the spirit of my first attempt
I seem to recall that the current version of implode treats alternatives as nodes and does not flatten them, even if the alternatives are merely literals. Your definition is perfect, nevertheless.
It's a relatively simple feature request imho, if you have time to register it on GitHub.
Another option is to not implode at all and use concrete syntax
I'm trying to automate some output using printf but I'm struggling to find a way to pass to it the list of arguments expr_1, ..., expr_n in
printf (dest, string, expr_1, ..., expr_n)
I thought of using something like Javascript's spread operator but I'm not even sure I should need it.
For instace, say I have a list of strings to be output
a:["foo","bar","foobar"];
a string of appropriate format descriptors, say
s: "~a ~a ~a ~%";
and an output stream, say os. How can I invoke printf using these things in such a way that the result will be the same as writing
printf(os,s,a[1],a[2],a[3]);
Then I could generalize it to output lists of variable size.
Any suggestions?
Thanks.
EDIT:
I just learned about apply and, using the conditions I posed in my OP, the following seems to work wonderfully:
apply(printf,append([os,s],a));
Maxima printf implements most or maybe all of the formatting operators from Common Lisp FORMAT, which are quite extensive; see: http://www.lispworks.com/documentation/HyperSpec/Body/22_c.htm See also ? printf in Maxima to get an abbreviated list of formatting operators.
In particular for a list you can do something like:
printf (os, "my list: ~{~a~^, ~}~%", a);
to get the elements of a separated by ,. Here "~{...~}" tells printf to expect a list, and ~a is how to format each element, ~^ means omit the inter-element stuff after the last element, and , means put that between elements. Of course , could be anything.
There are many variations on that; if that's not what you're looking for, maybe I can help you find it.
There's a common idiom for traversing a string whose characters may be escaped with a backslash by using the regex (\\.|.), like this:
alert( "some\\astring".replace(/(\\.|.)/g, "[$1]") )
That's in JavaScript. This code changes the string some\astring to [s][o][m][e][\a][s][t][r][i][n][g].
I know that Lua patterns don't support the OR operator, so we can't translate this regular expression directly into a Lua pattern.
Yet, I was wondering: is there an alternative way to do this (traversing possibly-escaped characters) in Lua, using a Lua pattern?
You can try
(\\?.)
and replace with [$1]
See it on Regexr.
? is a shortcut quantifier for 0 or 1 occurences, so the above pattern is matching a character and an optional backslash before. If ? is not working (I don't know lua) you can try {0,1} instead. That is the long version of the same.