首页 > 文档 > match()匹配函数
2017
07-21

match()匹配函数

Name

match()匹配函数

   

Examples

String s = “Inside a tag, you will find <tag>content</tag>.”;

String[] m = match(s, “<tag>(.*?)</tag>”);

println(“Found ‘” + m[1] + “‘ inside the tag.”);

// Prints to the console:

// “Found ‘content’ inside the tag.”

match()匹配函数 - 第1张  | Processing编程艺术

String s1 = “Have you ever heard of a thing called fluoridation. “;

s1 += “Fluoridation of water?”;

String s2 = “Uh? Yes, I-I have heard of that, Jack, yes. Yes.”;

 

String[] m1 = match(s1, “fluoridation”);

if (m1 != null) { // If not null, then a match was found

// This will print to the console, since a match was found.

println(“Found a match in ‘” + s1 + “‘”);

} else {

println(“No match found in ‘” + s1 + “‘”);

}

 

String[] m2 = match(s2, “fluoridation”);

if (m2 != null) {

println(“Found a match in ‘” + s2 + “‘”);

} else {

// This will print to the console, since no match was found.

println(“No match found in ‘” + s2 + “‘”);

}

Description

This function is used to apply a regular expression to a piece of text, and return matching groups (elements found inside parentheses) as a String array. If there are no matches, a null value will be returned. If no groups are specified in the regular expression, but the sequence matches, an array of length 1 (with the matched text as the first element of the array) will be returned.

To use the function, first check to see if the result is null. If the result is null, then the sequence did not match at all. If the sequence did match, an array is returned.

If there are groups (specified by sets of parentheses) in the regular expression, then the contents of each will be returned in the array. Element [0] of a regular expression match returns the entire matching string, and the match groups start at element [1] (the first group is [1], the second [2], and so on).

The syntax can be found in the reference for Java’s Pattern class. For regular expression syntax, read the Java Tutorial on the topic.

此函数用于将正则表达式应用于文本片断, 并将匹配组 (在括号内找到的元素) 返回为字符串数组。如果没有匹配项, 则返回 null 值。如果在正则表达式中未指定任何组, 但序列匹配, 则将返回长度为1的数组 (将匹配的文本作为数组的第一个元素)

 

若要使用该函数, 请首先检查结果是否为 null。如果结果为 null, 则序列根本不匹配。如果序列匹配, 则返回一个数组。

 

如果正则表达式中有组 (由圆括号集合指定), 则每个数组中的内容都将返回。正则表达式匹配项的元素 [0] 返回整个匹配字符串, 并且匹配组从元素 [1] 开始 (第一个组为 [1], 其次是 [2], 等等)

 

该语法可以在 java 的模式类的引用中找到。对于正则表达式语法, 请阅读有关该主题的 java 教程。

Syntax

match(str, regexp)

Parameters

str

String: the String to be searched

字符串: 要搜索的字符串

regexp

String: the regexp to be used for matching

 

字符串: 用于匹配的 regexp

Returns

String[]

Related

matchAll()
split()
splitTokens()
join()
trim()



最后编辑:
作者:卡萨布兰卡
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。