Lambda expressions and regular expressions

Lambda expressions

Lambda expressions support code blocks as method parameters. Lambda expressions allow the use of cleaner code to create instances of interfaces with only one abstract method (functional interfaces).

1. Getting Started with Lambda Expressions

Lambda expressions replace the cumbersome syntax of anonymous inner classes, do not need to indicate the name of the overridden method, nor the return value type of the overridden method, just give the parentheses of the overridden method and the formal parameter list in the parentheses That's it.

Lambda expression composition: formal parameter list, and allows to omit the parameter type, a parameter can omit parentheses; arrow->; code block, a statement can omit curly braces, a return statement can omit return.

 2. Lambda expressions and functional interfaces

The target type of a lambda expression must be a "functional interface", which represents an interface that contains only one abstract method. Lambda expressions are used when only one abstract method needs to be implemented to create an instance of a functional interface using anonymous inner class syntax. Lambda expressions can be used for assignment, because they can be treated as objects. The target type of a lambda expression must be an explicit functional interface, and an object can only be created for a functional interface, and only one method can be implemented. To make the target type of a lambda expression a clear functional interface, assign the lambda expression to a variable of the functional interface type; pass the lambda expression as a parameter of the functional interface type to a method; use A functional interface casts a lambda expression.

A typical functional interface under the java.uril.function package:

XxxFuncyion (transforms the specified data, and the processing logic of the conversion method is implemented by Lambda); XXXConsumer (similar to the above, processes parameters, but does not return the processing result); XXXSupplier (does not input parameters, according to a certain logic algorithm (by Lambda) Implementation) return data); XxxxPredicate (used to judge whether the parameter (implemented by Lambda) meets certain conditions and filter the data).

3. Method reference and constructor reference

If the code block of the lambda expression has only one code, you can use method references and constructor references in the code block.

reference class method

 Class name:: class method; all parameters of the implemented method in the functional interface are passed to the class method as parameters; (a, b, ...) - "class name. Class methods (a, b, ...).

The code block of a lambda expression has only one line of code that calls a class method, and can be replaced by a reference class method.

Such as: Converter converter1 = from- "Integer. valueOf(from) can be replaced by a reference class method Converter converter1 = Integer::valueOf;

An instance method that references a specific object

Specific object:: instance method; all parameters of the implemented method in the functional interface are passed to the method as parameters; (a, b, ...) - "specific object. Instance methods (a, b, ...).

The code block of the lambda expression is only one line of code that calls the indexOf() instance method of the "fkit.org" object, which can be replaced by the instance method that refers to the specific object.

Such as: Converter converter2 = from- ""fkit.org". indexOf(from); replace with Converter converter2 = "fkit.org"::indexOf;

referencing an instance method of an object of a class

Class name: Instance method; the first parameter of the implemented method in the functional interface is the caller, and all subsequent parameters are passed to the method as parameters; (a, b, ...) - "a. instance methods (b, . . . ).

The code block of a lambda expression has only one line a. substring(b, c) can be replaced by an instance method that references an object of a certain class.

Such as: MyTest mt = (a, b, c)->a. substring(b, c); replace with MyTest mt = String::substring;

reference constructor

Class name: new; all parameters of the implemented method in the functional interface are passed to the constructor as parameters; (a, b, ...) - "new. Class name (a, b, ...).

 The code block of the lambda expression is only one line new JFrame (a), which calls the constructor, which can be replaced by the reference constructor.

Such as: YourTest yt = (String a)->new JFrame(a); instead of YourTest yt = JFrame::new;

4. The connection and difference between Lambda expression and anonymous inner class

Contact: Lambda expressions, like anonymous inner classes, can directly access "effectively final" local variables and member variables of outer classes; the objects created by lambda expressions are the same as objects generated by anonymous inner classes, and can be directly called from Default methods inherited in interfaces.

Difference: Lambda expressions can only create instances for functional interfaces, while anonymous inner classes can create instances for arbitrary interfaces, abstract classes, and ordinary classes; Lambda expressions code blocks are not allowed to call the default methods defined in the interface, while anonymous inner classes Classes can.

5. Invoke class methods of Arrays using lambda expressions

Some methods of the Arrays class require instances of functional interfaces such as Comparator, XxxOperator, and XXXFunction. Lambda expressions can be used to make the code more concise.

Arrays.perallelsort(arr1,(o1,o2)->o1.length() - o2.length()) ; The target type is Comparator and specifies the criteria for judging the size of the string.

 

 

regular expression

A regular expression is a logical formula for manipulating strings (including ordinary characters (for example, letters between a and z) and special characters (called "metacharacters")) by using predefined specific characters , and the combination of these specific characters to form a "rule string", this "rule string" is used to express a filtering logic for strings. A regular expression is a text pattern that describes one or more strings to match when searching for text.

Regular expression is a powerful string processing tool that can search, extract, split, replace and other operations on strings.

Boolean matches (String regex): Determines whether the string matches the specified regular expression.

String replaceAll(String regex, String replacement): Replaces all substrings matching regex in this string with raplacement.

String replaceFirst(String regex, String replacement): Replaces the first substring matching regex in this string with raplacement.

String[] split(String regex): Use regex as the separator to split the string into multiple substrings.

All of the above rely on regular expression support.

A regular expression is a template for matching strings.

The characteristics of regular expressions are:

1. Very flexible, logical and functional;
2. The complex control of strings can be achieved quickly and in a very simple way.
3. For those who are new to it, it is more obscure and difficult to understand.
Since the main application is text, it is used in various text editors, ranging from the famous editor EditPlus to large editors such as Microsoft Word and Visual Studio, can use regular expressions to process text content.
 

To really use regular expressions well, the correct understanding of metacharacters is the most important thing.

metacharacter
describe
\
Converts the next character token, or a backreference, or an octal escape. For example, "\\n" matches \n. "\n" matches a newline. The sequence "\\" matches "\" and "\(" matches "(". This is equivalent to the concept of "escape character" found in many programming languages.
^
Matches the beginning of the input word line. If the Multiline property of the RegExp object is set, ^ also matches the position after "\n" or "\r".
$
Match end of input line. If the Multiline property of the RegExp object is set, $ also matches the position before "\n" or "\r".
*
Matches the preceding subexpression any number of times. For example, zo* matches "z", as well as "zo" and "zoo". *equivalent to o{0,}
+
Match the preceding subexpression one or more times (greater than or equal to 1). For example, "zo+" matches "zo" and "zoo", but not "z". + is equivalent to {1,}.
?
Matches the preceding subexpression zero or one time. For example, "do(es)?" can match "do" or "do" in "does". ? is equivalent to {0,1}.
{n}
n is a non-negative integer. Match a certain number of n times. For example, "o{2}" cannot match the "o" in "Bob", but can match the two o's in "food".
{n,}
n is a non-negative integer. Match at least n times. For example, "o{2,}" would not match the "o" in "Bob", but would match all o's in "foooood". "o{1,}" is equivalent to "o+". "o{0,}" is equivalent to "o*".
{n,m}
Both m and n are non-negative integers, where n <= m . Match at least n times and at most m times. For example, "o{1,3}" will match the first three o's in "fooooood" as a set, and the last three o's as a set. "o{0,1}" is equivalent to "o?". Note that there can be no spaces between the comma and the two numbers.
?
When the character immediately follows any one of the other qualifiers (*,+,?, { n }, { n ,}, { n , m }), the matching pattern is non-greedy. The non-greedy mode matches as little of the searched string as possible, while the default greedy mode matches as much of the searched string as possible. For example, for the string "oooo", "o+" will match as many "o" as possible, yielding the result ["oooo"], while "o+?" will match as little "o" as possible, yielding the result ['o ', 'o', 'o', 'o']
.point
Matches any single character except "\n". To match any character including "\n", use a pattern like "[\s\S]".
(pattern)
Match pattern and get that match. The retrieved matches can be obtained from the resulting Matches collection, using the SubMatches collection in VBScript and the $0…$9 properties in JScript. To match parentheses characters, use "\(" or "\)".
(?:pattern)
Non-fetching matches, matches the pattern but does not obtain the matching result, and does not store it for later use. This is useful when using the or character "(|)" to combine parts of a pattern. For example "industr(?:y|ies)" is a shorter expression than "industry|industries".
(?=pattern)
Non-acquisition matching, positive positive lookahead, matches the lookup string at the beginning of any string matching pattern, the match does not need to be acquired for later use. For example, "Windows(?=95|98|NT|2000)" can match "Windows" in "Windows2000", but not "Windows" in "Windows3.1". Lookahead consumes no characters, that is, after a match occurs, the search for the next match begins immediately after the last match, not after the character containing the lookahead.
(?!pattern)
Non-fetch matching, forward negative lookahead, matches the lookup string at the beginning of any string that does not match pattern, the match does not need to be fetched for later use. For example, "Windows(?!95|98|NT|2000)" can match "Windows" in "Windows3.1", but not "Windows" in "Windows2000".
(?<=pattern)
Non-acquisition matching, reverse positive pre-check, is similar to positive positive pre-check, but in the opposite direction. For example, "(?<=95|98|NT|2000)Windows" matches "Windows" in "2000Windows", but not "Windows" in "3.1Windows".
(?<!pattern)
Non-acquisition matches, reverse negative pre-checks, are similar to forward negative pre-checks, but in the opposite direction. For example, "(?<!95|98|NT|2000)Windows" can match "Windows" in "3.1Windows", but not "Windows" in "2000Windows". This place is incorrect, there is a problem
Any item used here cannot exceed 2 digits, such as "(?<!95|98|NT|20) Windows is correct, "(?<!95|980|NT|20) Windows reports an error, if it is used alone, then Unlimited, eg (?<!2000) Windows matches correctly
x|y
matches x or y. For example, "z|food" can match "z" or "food" (be careful here). "[zf]ood" matches "zood" or "food".
[xyz]
character collection. Matches any one of the included characters. For example, "[abc]" can match "a" in "plain".
[^xyz]
A collection of negative characters. Matches any character not included. For example, "[^abc]" can match "plin" in "plain".
[a-z]
character range. Matches any character in the specified range. For example, "[az]" matches any lowercase alphabetic character in the range "a" to "z".
Note: Only when the hyphen is inside a character group and appears between two characters can it represent a range of characters; if it is out of the beginning of a character group, it can only represent the hyphen itself.
[^a-z]
负值字符范围。匹配任何不在指定范围内的任意字符。例如,“[^a-z]”可以匹配任何不在“a”到“z”范围内的任意字符。
\b
匹配一个单词边界,也就是指单词和空格间的位置(即正则表达式的“匹配”有两种概念,一种是匹配字符,一种是匹配位置,这里的\b就是匹配位置的)。例如,“er\b”可以匹配“never”中的“er”,但不能匹配“verb”中的“er”。
\B
匹配非单词边界。“er\B”能匹配“verb”中的“er”,但不能匹配“never”中的“er”。
\cx
匹配由x指明的控制字符。例如,\cM匹配一个Control-M或回车符。x的值必须为A-Z或a-z之一。否则,将c视为一个原义的“c”字符。
\d
匹配一个数字字符。等价于[0-9]。grep 要加上-P,perl正则支持
\D
匹配一个非数字字符。等价于[^0-9]。grep要加上-P,perl正则支持
\f
匹配一个换页符。等价于\x0c和\cL。
\n
匹配一个换行符。等价于\x0a和\cJ。
\r
匹配一个回车符。等价于\x0d和\cM。
\s
匹配任何不可见字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
\S
匹配任何可见字符。等价于[^ \f\n\r\t\v]。
\t
匹配一个制表符。等价于\x09和\cI。
\v
匹配一个垂直制表符。等价于\x0b和\cK。
\w
匹配包括下划线的任何单词字符。类似但不等价于“[A-Za-z0-9_]”,这里的"单词"字符使用Unicode字符集。
\W
匹配任何非单词字符。等价于“[^A-Za-z0-9_]”。
\xn
匹配n,其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,“\x41”匹配“A”。“\x041”则等价于“\x04&1”。正则表达式中可以使用ASCII编码。
\num
匹配num,其中num是一个正整数。对所获取的匹配的引用。例如,“(.)\1”匹配两个连续的相同字符。
\n
标识一个八进制转义值或一个向后引用。如果\n之前至少n个获取的子表达式,则n为向后引用。否则,如果n为八进制数字(0-7),则n为一个八进制转义值。
\nm
标识一个八进制转义值或一个向后引用。如果\nm之前至少有nm个获得子表达式,则nm为向后引用。如果\nm之前至少有n个获取,则n为一个后跟文字m的向后引用。如果前面的条件都不满足,若nm均为八进制数字(0-7),则\nm将匹配八进制转义值nm
\nml
如果n为八进制数字(0-7),且ml均为八进制数字(0-7),则匹配八进制转义值nml
\un
匹配n,其中n是一个用四个十六进制数字表示的Unicode字符。例如,\u00A9匹配版权符号(&copy;)。
\p{P}
小写 p 是 property 的意思,表示 Unicode 属性,用于 Unicode 正表达式的前缀。中括号内的“P”表示Unicode 字符集七个字符属性之一:标点字符。
其他六个属性:
L:字母;
M:标记符号(一般不会单独出现);
Z:分隔符(比如空格、换行等);
S:符号(比如数学符号、货币符号等);
N:数字(比如阿拉伯数字、罗马数字等);
C:其他字符。
*注:此语法部分语言不支持,例:javascript。
\<
\>
匹配词(word)的开始(\<)和结束(\>)。例如正则表达式\<the\>能够匹配字符串"for the wise"中的"the",但是不能匹配字符串"otherwise"中的"the"。注意:这个元字符不是所有的软件都支持的。
( ) 将( 和 ) 之间的表达式定义为“组”(group),并且将匹配这个表达式的字符保存到一个临时区域(一个正则表达式中最多可以保存9个),它们可以用 \1 到\9 的符号来引用。
| 将两个匹配条件进行逻辑“或”(Or)运算。例如正则表达式(him|her) 匹配"it belongs to him"和"it belongs to her",但是不能匹配"it belongs to them."。注意:这个元字符不是所有的软件都支持的。

 

 正则表达式应用——实例应用

1.验证用户名和密码:("^[a-zA-Z]\w{5,15}$")正确格式:"[A-Z][a-z]_[0-9]"组成,并且第一个字必须为字母6~16位;
 
2.验证电话号码:("^(\d{3,4}-)\d{7,8}$")正确格式:xxx/xxxx-xxxxxxx/xxxxxxxx;
 
3.验证手机号码:"^1[3|4|5|7|8][0-9]{9}$";
 
4.验证身份证号(15位):"\d{14}[[0-9],0-9xX]",(18位):"\d{17}[[0-9],0-9xX]";
 
5.验证Email地址:("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
 
6.只能输入由数字和26个英文字母组成的字符串:("^[A-Za-z0-9]+$");
 
7.整数或者小数:^[0-9]+([.][0-9]+){0,1}$
 
8.只能输入数字:"^[0-9]*$"。
 
9.只能输入 n位的数字:"^\d{ n}$"。
 
10.只能输入至少 n位的数字:"^\d{ n,}$"。
 
11.只能输入 m~ n位的数字:"^\d{ m, n}$"。
 
12.只能输入零和非零开头的数字:"^(0|[1-9][0-9]*)$"。
 
13.只能输入有两位小数的正实数:"^[0-9]+(\.[0-9]{2})?$"。
 
14.只能输入有1~3位小数的正实数:"^[0-9]+(\.[0-9]{1,3})?$"。
 
15.只能输入非零的正整数:"^\+?[1-9][0-9]*$"。
 
16.只能输入非零的负整数:"^\-[1-9][0-9]*$"。
 
17.只能输入长度为3的字符:"^.{3}$"。
 
18.只能输入由26个英文字母组成的字符串:"^[A-Za-z]+$"。
 
19.只能输入由26个大写英文字母组成的字符串:"^[A-Z]+$"。
 
20.只能输入由26个小写英文字母组成的字符串:"^[a-z]+$"。
 
21.验证是否含有^%&',;=?$\"等字符:"[%&',;=?$\\^]+"。
 
22.只能输入汉字:"^[\u4e00-\u9fa5]{0,}$"。
 
23.验证URL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。
 
24.验证一年的12个月:"^(0?[1-9]|1[0-2])$"正确格式为:"01"~"09"和"10"~"12"。
 
25.验证一个月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正确格式为;"01"~"09"、"10"~"29"和“30”~“31”。
 
26.获取日期正则表达式:\\d{4}[年|\-|\.]\d{\1-\12}[月|\-|\.]\d{\1-\31}日?
评注:可用来匹配大多数年月日信息。
 
27.匹配双 字节 字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
 
28.匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行
 
29.匹配HTML标记的正则表达式:<(\S*?)[^>]*>.*?</>|<.*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
 
30.匹配首尾空白 字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、 制表符、换页符等等),非常有用的表达式
 
31.匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
 
32.匹配帐号是否合法(字母开头,允许5-16 字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用
 
33.匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10 000 开始
 
34.匹配中国邮政编码:[1-9]\\d{5}(?!\d)
评注:中国邮政编码为6位数字
 
35.匹配ip地址:([1-9]{1,3}\.){3}[1-9]。
评注:提取ip地址时有用
 
36.匹配MAC地址:([A-Fa-f0-9]{2}\:){5}[A-Fa-f0-9]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324957495&siteId=291194637