what is the difference between replace, replaceAll and replaceAllLiterally in scala? - scala

what is the difference between replace, replaceAll and replaceAllLiterally in Scala or Java?

replace and replaceAllLiterally are similar, and in fact the latter is deprecated. From docs:
(Since version 2.13.2) Use s.replace as an exact replacement
replaceAll on the other hand matches the string with regex and replaces it with the replacement string.

Related

What's the common denominator for regex "pattern" in OpenAPI?

I'm using FastAPI, which allows pattern=re.compile("(?P<foo>[42a-z]+)...").
https://editor.swagger.io/ shows an error for this pattern.
My guess is that Python's named group syntax (?P<name>...) is different from ES2018 (?<name>...).
But, come to think of it, the idea of OpenAPI is interoperability, and some other language, esp. a compiled language may use yet another notation, or may not support named groups in the regular expressions at all.
What common denominator of regular expression syntax should I use?
OpenAPI uses json schema, and the json schema spec defines regex as "A regular expression, which SHOULD be valid according to the ECMA-262 regular expression dialect." Here is the relevant ECMA-262 section.
Of course non-javascript implementations probably won't care too much about it, and just use the default regex library of their platform. So good luck with figuring out the common denominator :)
I suggest just using as simple regexes as possible. And add some tests for it, using the library that you use in production.
Json Schema recommends a specific subset of regular expressions because the authors accept that most implementations will not support full ECMA 262 syntax:
https://json-schema.org/understanding-json-schema/reference/regular_expressions.html
A single unicode character (other than the special characters below) matches itself.
.: Matches any character except line break characters. (Be aware that what constitutes a line break character is somewhat dependent on your platform and language environment, but in practice this rarely matters).
^: Matches only at the beginning of the string.
$: Matches only at the end of the string.
(...): Group a series of regular expressions into a single regular expression.
|: Matches either the regular expression preceding or following the | symbol.
[abc]: Matches any of the characters inside the square brackets.
[a-z]: Matches the range of characters.
[^abc]: Matches any character not listed.
[^a-z]: Matches any character outside of the range.
+: Matches one or more repetitions of the preceding regular expression.
*: Matches zero or more repetitions of the preceding regular expression.
?: Matches zero or one repetitions of the preceding regular expression.
+?, *?, ??: The *, +, and ? qualifiers are all greedy; they match as much text as possible. Sometimes this behavior isn’t desired and you want to match as few characters as possible.
(?!x), (?=x): Negative and positive lookahead.
{x}: Match exactly x occurrences of the preceding regular expression.
{x,y}: Match at least x and at most y occurrences of the preceding regular expression.
{x,}: Match x occurrences or more of the preceding regular expression.
{x}?, {x,y}?, {x,}?: Lazy versions of the above expressions.
P.S. Kudos to #erosb for the idea how to find this recommendation.

Verbatim strings in PureScript

Does PureScript support verbatim string literals? Something like #"regex \s no escapes" in C#.
Alternatively is there support for regex literals as in JavaScript?
You can use triple-quote strings.
There are no regex literals AFAIK, but triple-quote strings might help there too. Example from docs:
regex """.+#.+\..+""" noFlags

Looking for method equivalent to find previous index of character in string in IOS

I have an string with some special characters.
The thing is, I want to search those special characters from the end of the string.
Is there any method available to search the index of character in backward direction?
Yes, use rangeOfCharacterFromSet:options:range: and use NSBackwardsSearch for the options argument.

how to treat * as string and not as wildcard in lucene.net

I have a term test* and I want to treat it as string and not a wildcard of test.
How will I be able to do that in Lucene.Net. Any help???
Yes you can use a backslash to escape special characters. Both in the QueryParser and custom built searches. List of characters that require escaping can be found here.
If you're using the newer versions of Lucene.Net, you can use QueryParser.Escape("test*") to escape your search term. QueryParser.Escape() takes a string and returns the string after properly escaping all characters that are special for Lucene.

Wildcards in strings

I'm interested in using rangeOfString with some wildcard characters as part of a search string.
For example, if I have several strings like "244px" and "356px" and I want to convert all such strings to "320px". Is there a way I can use wildcards to get the desired result?
If you could use regex, you can do substitution for this pattern "[1-9][0-9]+px" to "320px"
RegexKitLite is what you want, it has a small NSString extenstion class that lets you use the built-in regex libraries easily.
Use NSNumberFormatter configured to the right pattern.