How to escape mask rules in kendo maskedtextbox for angular2? - kendo-ui-angular2

I need to build a mask where some charachters are fixed, like a number where the user have to specify last three digits.
How can I escape the "0" default mask behaviour?
the docs says to use backslash character, but I can't figure out how it should works.
example: my mask needs to show the number 12000___ and user have to enter last 3 digits
html:
<kendo-maskedtextbox [mask]="mask"></kendo-maskedtextbox>
ts:
mask = "12\0\0\0000";
but it doesn't compile...

A backslash is a escape character in (JavaScript) strings, thus you need to escape the backslash.
Like this: mask = "12\\0\\0\\0000"
I've also prepared a Plunkr.

Related

Regex: How to mark multiple points but ignore points embedded in letters?

I am facing a matching problem...
I am trying to mark the multiple dots in an incorrect mail address so that only the relevant dots appear:
mike.smith#yahoo..com
mike....smith#yahoo.com
My approach (\.(?!(\.\w))) works correctly for several dots in a row, but still matches the single dot in mike.smith#yahoo.com
Do you have an idea?
Any help appreciated.
If you want to match those dots in an email address assuming there should be at least an # char present, and using a lookbehind is supported:
Note that this does not validate an email format.
\.(?=\.[^#\s]*#)|(?<=#[^\s#]*\.)\.
Explanation
\. Match a dot
(?=\.[^#\s]*#) Positive lookahead, assert a dot directly to the right and an # char to the right without crossing whitespace chars
| or
(?<=#[^\s#]*\.) Assert an # char to the left and a dot directly before matching the dot
\. Match a dot
Regex demo
\.{2,} would match two or more consecutive dots.
https://regex101.com/r/In9Qzm/1
You can use
\.(?=\.+\w)
See the regex demo.
Details:
\. - a dot
(?=\.+\w) - a positive lookahead that matches a location that is immediately followed with one or more dots and then a word char.
Note that \.\B will remove any dot that is either at the end of the string or is followed with a non-word char (i.e. a char other than a letter, digit or underscore).

How to split strings sperated by comma with escapes?

I have a string looks like this:
(The whole code block is a string, aka, this string contains quotation marks.)
"he\"llo", "world\n", "fro,m"
[update] Aka, the "actual" string is this:
"\"he\\\"llo\", \"world\\n\", \"fro,m\""
I want to get an array of strings like this:
[ "\"he\\\"llo\"", "\"world\\n\"", "\"fro,m\"" ]
[update] Comma inside quotation marks should be remained.
In my opinion, there are several ways to solve this:
build a automata (DFA or NFA) for this syntax
using several status flags like inQuote, handle judging logics with lots of if else
write a complex but clever Regular Expression for this
Are there any general solutions to this problem? Or how should I actually do using those thinkings above?
P.S. It couldn't be better if some syntax errors like "unclosed quotation mark" can be found.
You need to first define your grammar. This is a simple grammar for your case:
document = *WS [string *WS *(',' *WS string *WS)]
string = %x22 *char %x22
char = %x20-21 / %x23-5B / escape / %x5D-10FFFF
escape = %x5C (%x5C / %x22 / 't' / 'n' / 'r')
WS = %x9 / %x20
You can read it as:
A document may begin/end with a white space, then may have one or more strings separated by commas. Before and after each comma there may be some white space.
A string is made of characters and begins and ends with double quotes Unicode/ASCII hex code 22.
Each character (char), may be: 1) any non control Unicode character before the double quotes i.e. hex 20 (space) or hex 21 (exclamation mark); 2) any character after the double quotes and before the escape slash \ (hex 5C); 3) an escape character sequence; 4) any other Unicode character after the slash (hex 5C).
The escape sequence (rule escape) begins with the escape slash \ and is followed by another slash, or the characters t for tab, n for line feed and r for carriage return. You may add and other escapable characters if you want, as for a C++ string syntax you can see here: https://en.cppreference.com/w/cpp/language/escape .
A white space (WS) is a tab or space, you may add and %xA and %xD for line feed and carriage return respectively.
By the use of this grammar you will get this tree for your input:
The screenshort is from the Tunnel Grammar Studio online laboratory that can run ABNF grammars (as the one above), and I work on.
After you have the grammar, you may use tools to generate a parser, or you can write one yourself. If you want to do it by hand (preferable for so small and simple grammar), you may have one function per each grammar rule that reads one character and checks is it the expected one. If your input finishes when you are parsing the string rule, then you have an input with a started but not finished string.
Your actual string syntax tree will look like that:

Does the order of AWK delimiters affects the segmentation result

my file content looks like this:
why those actions are different?
The field delimiter -F takes a regex and you're passing it something common, a character class for possible separators.
What happens in your second case is that in a character class, the - has the special meaning of defining a range when not as first or last entry: [a-z] means any character between a and z, so your second option [ -:] means "Use any character between space and : as separator, if you wish to see which characters this relies to you can check the ASCII table. One workaround is to espace it like [ \-:] but it's usually better to place it in a place where it doesn't have a special meaning like first or last place, the only character really needing to be escaped in a character class is ] as its place can't tell if it's closing the class or if it is part of it.
The resulting character set to split on is !\"#$%&'()*+,-./0123456789:

Allow more characters in string:match()

string:match( "[^%w%s]" )
This currently allows only letters, numbers and spaces as far as I understand. How can I add characters to it? For example I'd like to allow ( and _ too.
string:match( "[^%w%s(_]" )
This would add the characters you mentioned. Basically any character you add in the square brackets.
Some need to be escaped, such as parentheses.
^ in the beginning of a char-set means to get the complement. In your example, [^%w%s] matches a character that isn't a letter/digit(%w), or a whitespace %s.
If you need to add ( and _, just add them: [^%w%s(_]

Characters to separate value

i need to create a string to store couples of key/value data, for example:
key1::value1||key2::value2||key3::value3
in deserializing it, i may encounter an error if the key or the value happen to contain || or ::
What are common techniques to deal with such situation? thanks
A common way to deal with this is called an escape character or qualifier. Consider this Comma-Separated line:
Name,City,State
John Doe, Jr.,Anytown,CA
Because the name field contains a comma, it of course gets split improperly and so on.
If you enclose each data value by qualifiers, the parser knows when to ignore the delimiter, as in this example:
Name,City,State
"John Doe, Jr.",Anytown,CA
Qualifiers can be optional, used only on data fields that need it. Many implementations will use qualifiers on every field, needed or not.
You may want to implement something similar for your data encoding.
Escape || when serializing, and unescape it when deserializing. A common C-like way to escape is to prepend \. For example:
{ "a:b:c": "foo||bar", "asdf": "\\|||x||||:" }
serialize => "a\:b\:c:foo\|\|bar||asdf:\\\\\|\|\|x\|\|\|\|\:"
Note that \ needs to be escaped (and double escaped due to being placed in a C-style string).
If we assume that you have total control over the input string, then the common way of dealing with this problem is to use an escape character.
Typically, the backslash-\ character is used as an escape to say that "the next character is a special character", so in this case it should not be used as a delimiter. So the parser would see || and :: as delimiters, but would see \|\| as two pipe characters || in either the key or the value.
The next problem is that we have overloaded the backslash. The problem is then, "how do I represent a backslash". This is sovled by saying that the backslash is also escaped, so to represent a \, you would have to say \\. So the parser would see \\ as \.
Note that if you use escape characters, you can use a single character for the delimiters, which might make things simpler.
Alternatively, you may have to restict the input and say that || and :: are just baned and fail/remove when the string is encoded.
A simple solution is to escape a separator (with a backslash, for instance) any time it occurs in data:
Name,City,State
John Doe\, Jr.,Anytown,CA
Of course, the separator will need to be escaped when it occurs in data as well; in this case, a backslash would become \\.
You can use non-ascii character as separator (e.g. vertical tab :-) ).
You can escape separator character in your data during serialization. For example: if you use one character as separator (key1:value1|key2:value2|...) and your data is:
this:is:key1 this|is|data1
this:is:key2 this|is|data2
you double every colon and pipe character in you data when you serialize it. So you will get:
this::is::key1:this||is||data1|this::is::key2:this||is||data2|...
During deserialization whenever you come across two colon or two pipe characters you know that this is not your separator but part of your data and that you have to change it to one character. On the other hand, every single colon or pipe character is you separator.
Use a prefix (say "a") for your special characters (say "b") present in the key and values to store them. This is called escaping.
Then decode the key and values by simply replacing any "ab" sequence with "b". Bear in mind that the prefix is also a special character. An example:
Prefix: \
Special characters: :, |, \
Encoded:
title:Slashdot\: News for Nerds. Stuff that Matters.|shortTitle:\\.
Decoded:
title=Slashdot: News for Nerds. Stuff that Matters.
shortTitle=\.
The common technique is escaping reserved characters, for example:
In urls you escape some characters
using %HEX representation:
http://example.com?aa=a%20b
In programming languages you escape
some characters with a slash prefix:
"\"hello\""

Resources