Swift Regex with special Characters - ios

I am attempting to use this string:
var passwordRegex = "^[A-Za-z0-9 !\"#$%&'()*+,-./:;<=>?#[\\]^_`{|}~].{8,}$"
As my regular expression pattern, but it keeps failing saying the pattern is invalid. I used the \ character to escape for the characters: " and \, but it throws the error: invalid escape sequence in literal for regex key characters like ^ & [ ] | . etc.
What am I missing in order to allow the characters:
! " # $ % & ' ( ) * + , - . / : ; < = > ? # [ \ ] ^ _ ` { | } ~
(including space) in my regex? I assume it's something with how I am escaping, but I can't find anything anywhere for these characters in regards to SWIFT's regular expression.

The problem is that characters [, \, and ] need to be escaped because they have special meaning in a regular expression.
So you need \[, \\, and \] in the regular expression. But since this is inside a Swift string, each \ needs to be escaped with a \.
So [\] becomes \[\\\] in the regular expression which becomes \\[\\\\\\] in the Swift string.
The final valid string is:
var passwordRegex = "^[A-Za-z0-9 !\"#$%&'()*+,-./:;<=>?#\\[\\\\\\]^_`{|}~].{8,}$"

Related

Print double quotes in Forth

The word ." prints a string. More precisely it compiles the (.") and the string up to the next " in the currently compiled word.
But how can I print
That's the "question".
with Forth?
In a Forth-2012 System (e.g. Gforth) you can use string literals with escaping via the word s\" as:
: foo ( -- ) s\" That's the \"question\"." type ;
In a Forth-94 system (majority of standard systems) you can use arbitrary parsing and the word sliteral as:
: foo ( -- ) [ char | parse That's the "question".| ] sliteral type ;
A string can be also extracted up to the end of the line (without printable delimiter); a multi-line string can be extracted too.
Specific helpers for particular cases can be easily defined.
For example, see the word s$ for string literals that are delimited by any arbitrary printable character, e.g.:
s$ `"test" 'passed'` type
Old school:
34 emit
Output:
"
Using gforth:
: d 34 emit ;
cr ." That's the " d ." question" d ." ." cr
Output:
That's the "question".

Ignoring backslash and double quotes in F#

How do we ignore backslashes in a string?
I tried this but it dosen't work:
let str2 = #"I igonore \ \ \ / / / / backsalshes"
printfn "%s" str2
Also, I thought ignoring double quotes in a string is as follow:
let str3 = """ "I ig""onore double quotes and backslasehes " """
printfn "%s" str3
Again, It printed a string with the double quote 'ig""onore'.
What is wrong?
The # sign doesn't "ignore" backslashes in the sense that they don't appear in the string, it simply treats them as normal characters, instead of as characters with a special meaning. E.g.,
let s1 = "\n" # A single newline character
let s2 = #"\n" # Two characters, a backslash and then a lowercase n
Likewise, the triple-quote syntax doesn't make " characters disappear, it simply lets you embed single " characters, or pairs of them, in the string without having to jump through syntax hoops to do so. If it make " characters disappear in a string, it wouldn't be very useful.

How to split by special character "\" in Lua?

I tried to split by "\", but this character is so special in Lua, even if I use escape character "%", the IDE shows an error Unterminated String constant
local index = string.find("lua. is \wonderful", "%\", 1)
To insert backslash \ into a quoted string, escape it with itself: "\\". \ is the escape character in regular quoted strings, so it is escaped with \. Or you can use the long string syntax, which doesn't allow escape sequences, as already pointed out: [[\]].
Percent is only an escape character in a string that is being used as a pattern, so it is used before the magical characters ^$()%.[]*+-? in the second argument to string.find, string.match, string.gmatch, and string.gsub, and %% represents % in the third argument to string.gsub.
The percent is still there in the string that is stored in memory, but backslash escape sequences are replaced with the corresponding character. \\ becomes \ when the string is stored in memory, and if you count the number of backslashes in a string "\\" using string.gsub, it will only find one: select(2, string.gsub("\\", "\\", "")) returns 1.

How pass string separated by spaces parameter to Jenkins build trigger?

This works so far:
http://JENKINS_SERVER/job/YOUR_JOB_NAME/buildWithParameters?myparam=Hello
But when the value of myparam contains whitespaces like in Hello word it doesn't work:
myparam=Hello word
Full line:
http://JENKINS_SERVER/job/YOUR_JOB_NAME/buildWithParameters?myparam=Hello world
How can I pass this parameter value?
You just need to replace the blank space by %20 :
http://JENKINS_SERVER/job/YOUR_JOB_NAM/buildWithParameters?myparam=Hello%20world
This is known as Url Enconding used for unsafe or special characters.
This is a summary table :
character encoded equivalence
backspace %08
tab %09
space %20
! %21
" %22
# %23
$ %24
% %25
& %26
' %27
( %28
) %29
* %2A
+ %2B
, %2C
- %2D
. %2E
/ %2F
: %3A
; %3B
< %3C
= %3D
> %3E
? %3F
# %40
[ %5B
\ %5C
] %5D
^ %5E
_ %5F
` %60
{ %7B
| %7C
} %7D
¿ %BF
References:
https://blogs.msdn.microsoft.com/oldnewthing/20100331-00/?p=14443
https://perishablepress.com/stop-using-unsafe-characters-in-urls/
complete url encoded values : https://www.degraeve.com/reference/urlencoding.php

How to replace double quotes in Erlang

This is probably a rather trivial question for the Erlang experts - I'm trying to have my ejabberd server store offline messages (in a Riak db) which inherently do contain double quotes (") around various values, etc. I get a format error when I try to create a Riak database object from them, and testing of replacing the double quotes with an escape character (\") corrects the issue. The question is how can I do this replacement manually?
I tried the following code but somehow doesn't work.
(ejabberd#xxx-xx-xx-xxx)4> re:replace(""hello"", """, "\"", [{return, list}, global]).
* 1: syntax error before: hello
So essentially I'm trying to replace the embedded " around the hello word with \".
I don't know Erlang, but you probably need something like this:
"\"hello\"", "\"", "\\\""
You must escape both " and \ in replacement string.
The Erlang literal syntax for strings uses the "\" (backslash)
character as an escape code. You need to escape backslashes in literal
strings, both in your code and in the shell, with an additional
backslash, i.e.: "\".
Example:
Let's make an example. I use $ Erlang symbol which will be substituted with ascii integer of a character to show what is happening behind each string which basically is a list of integer.
Subject = [$"] ++ "hello" ++ [$"] = "\"hello\"".
Target = [$"] = "\"".
Replacement = [$\\, $\\, $"] = "\\\\\"".
Result = re:replace(Subject, Target, Replacement, [{return, list}, global]).
Now with getting the length of Subject and Result we can find the difference:
7 = length(Subject). %% => 7 characters: " h e l l o "
9 = length(Result). %% => 9 characters: \ " h e l l o \ "

Resources