Racket Regex / String Replace - racket

is there a built-in function in drracket which allows me to check if a specific data is included in a string?
For example I want to get back a result "true" for any string with the letter "a" in them.
"fate" -> true
"fote" -> false
thank you

Racket includes a regular expressions package. You could use
(regexp-match <pattern> <input>)
There are probably implementations of substring? available, like in SRFI-13.

Related

Regex pattern to find force unwrapped variables

I want to find all the force unwrapped variables in my Xcode project. For example anything that's similar to:
variableName!.property
or
variableName!,
or
variableName! : otherVariable
or
variableName!)
Or any other similar occurrences of force unwrapped variables. What would be a regex pattern for that that I can use in the Xcode search?
This one will search for only valid variable names (alphanumeric strings starting with a letter) that are followed by an ! which is then followed by a space, tab, newline, or a period, comma, colon, or closing parenthesis . This search also excludes finding instances of try! and as!.
([A-z]+[A-Za-z0-9]*(?<![(try)(as)])![.,:)\n\t\r ])
This next pattern will match try! and as! if you are interested in finding those as well.
([A-z]+[A-Za-z0-9]*![.,:)\n\t\r ])
It should be noted both of these patterns will also match for variable types that are force unwrapped optionals (a common variable to be force unwrapped being #IBOutlets)
A really good resource for writing and testing regular expressions is regexr.com
Not fool-proof (that would require a full reading of the Swift syntax) but good enough for most cases:
\w[\w\d]*!
Of course you can simply search for ! - there are only a couple uses other than force unwrap: negative test (!=) and boolean (!valid). You may scoop up some string literals in the search, but unless you are writing an automatic tool, it hardly matters.
You should search .+!(\.|,| :|\)) using the Find > Regular Expression tool

Lisp return String of Symbol name

Basically I'm looking for a function that does the opposite of the following.
(intern "CAR")
This question is related. In Common Lisp, is there a function that returns a symbol from a given string?
The operator you are looking for is string (see the manual):
(string (intern "CAR"))
returns "CAR".
Either string or symbol-name would work to get the name of a symbol.
If you know specifically that you're passing n a symbol, symbol-name might allow both a compiler to generate better code as well as signal to a human reader that the argument is expected to be a symbol.

How to use regex to include/exclude some input files in sc.textFile?

I have attempted to filter out dates for specific files using Apache spark inside the file to RDD function sc.textFile().
I have attempted to do the following:
sc.textFile("/user/Orders/201507(2[7-9]{1}|3[0-1]{1})*")
This should match the following:
/user/Orders/201507270010033.gz
/user/Orders/201507300060052.gz
Any idea how to achieve this?
Looking at the accepted answer, it seems to use some form of glob syntax. It also reveals that the API is an exposure of Hadoop's FileInputFormat.
Searching reveals that paths supplied to FileInputFormat's addInputPath or setInputPath "may represent a file, a directory, or, by using glob, a collection of files and directories". Perhaps, SparkContext also uses those APIs to set the path.
The syntax of the glob includes:
* (match 0 or more character)
? (match single character)
[ab] (character class)
[^ab] (negated character class)
[a-b] (character range)
{a,b} (alternation)
\c (escape character)
Following the example in the accepted answer, it is possible to write your path as:
sc.textFile("/user/Orders/2015072[7-9]*,/user/Orders/2015073[0-1]*")
It's not clear how alternation syntax can be used here, since comma is used to delimit a list of paths (as shown above). According to zero323's comment, no escaping is necessary:
sc.textFile("/user/Orders/201507{2[7-9],3[0-1]}*")

How do i convert a string to a quoted variable

Lets say i want to get the documentation for a function, I'd say
(documentation 'foo 'function)
but what if I only had foo and function as strings? E.g. "foo" and "function".
What would I have to do to them to make them usable as parameters to the documentation call?
[Side note: I'm using clisp, but I doubt that matters.]
Use FIND-SYMBOL, not INTERN. If you want to find documentation for an existing function, finding a symbol is enough. INTERN also creates symbols.
CL-USER > (find-symbol "SIN" "COMMON-LISP")
SIN
:EXTERNAL
Note that Common Lisp symbols are uppercase internally be default. Thus you need to use an uppercase string to find the corresponding symbol in the corresponding package.
Also note that there actually isn't something like a 'quoted variable'. You want to convert a string to a symbol.
Use INTERN to convert a string to a symbol. Make sure you uppercase the strings because, unlike with symbols, the reader will not do it for you:
(tested in SBCL):
* (documentation 'mapcar 'function)
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
return values."
* (documentation (intern "MAPCAR") (intern "FUNCTION"))
"Apply FUNCTION to successive elements of LIST. Return list of FUNCTION
return values."

Can actions in Lex access individual regex groups?

Can actions in Lex access individual regex groups?
(NOTE: I'm guessing not, since the group characters - parentheses - are according to the documentation used to change precedence. But if so, do you recommend an alternative C/C++ scanner generator that can do this? I'm not really hot on writing my own lexical analyzer.)
Example:
Let's say I have this input: foo [tagName attribute="value"] bar and I want to extract the tag using Lex/Flex. I could certainly write this rule:
\[[a-z]+[[:space:]]+[a-z]+=\"[a-z]+\"\] printf("matched %s", yytext);
But let's say I would want to access certain parts of the string, e.g. the attribute but without having to parse yytext again (as the string has already been scanned it doesn't really make sense to scan part of it again). So something like this would be preferable (regex groups):
\[[a-z]+[[:space:]]+[a-z]+=\"([a-z]+)\"\] printf("matched attribute %s", $1);
You can separate it to start conditions. Something like this:
%x VALUEPARSE ENDSTATE
%%
char string_buf[100];
<INITIAL>\[[a-z]+[[:space:]]+[a-z]+=\" {BEGIN(VALUEPARSE);}
<VALUEPARSE>([a-z]+) (strncpy(string_buf, yytext, yyleng);BEGIN(ENDSTATE);} //getting value text
<ENDSTATE>\"\] {BEGIN(INITIAL);}
%%
About an alternative C/C++ scanner generator - I use QT class QRegularExpression for same things, it can very easy get regex group after match.
Certainly at least some forms of them do.
But the default lex/flex downloadable from sourceforge.org do not seem to list it in their documentation, and this example leaves the full string in yytext.
From IBM's LEX documentation for AIX:
(Expression)
Matches the expression in the parentheses.
The () (parentheses) operator is used for grouping and causes the expression within parentheses to be read into the yytext array. A group in parentheses can be used in place of any single character in any other pattern.
Example: (ab|cd+)?(ef)* matches such strings as abefef, efefef, cdef, or cddd; but not abc, abcd, or abcdef.