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

matchAll()全文匹配函数

Name

matchAll()全文匹配函数

   

Examples

String s = “Inside tags, you will find <tag>multiple</tag> “;

s += “<tag>pieces</tag> of <tag>content</tag>.”;

 

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

for (int i = 0; i < m.length; i++) {

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

}

 

// Prints to the console:

// “Found ‘multiple’ inside a tag.”

// “Found ‘pieces’ inside a tag.”

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

Description

This function is used to apply a regular expression to a piece of text, and return a list of matching groups (elements found inside parentheses) as a two-dimensional 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, a two dimensional array is still returned, but the second dimension is only of length one.

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, a 2D 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. Assuming a loop with counter variable i, element [i][0] of a regular expression match returns the entire matching string, and the match groups start at element [i][1] (the first group is [i][1], the second [i][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.

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

 

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

 

如果正则表达式中有组 (由圆括号集合指定), 则每个数组中的内容都将返回。假设一个具有计数器变量 i 的循环, 正则表达式匹配的元素 [i] [0] 返回整个匹配字符串, 并且匹配组从元素 [i] [1] (第一个组为 [i] [1], 第二个 [i] [2], 等等) 开始。

 

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

Syntax

matchAll(str, regexp)

Parameters

str

String: the String to be searched

字符串: 要搜索的字符串

regexp

String: the regexp to be used for matching

 

字符串: 用于匹配的 regexp

Returns

String[][]

Related

match()
split()
splitTokens()
join()
trim()



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

留下一个回复

你的email不会被公开。