Scripting Guide > Types of Data > Regular Expressions > Backreferences and Capturing Groups
发布日期: 08/07/2020

Backreferences and Capturing Groups

A regular expression can consist of patterns grouped in parentheses, also known as capturing groups. In ([a-zA-Z])\s([0-9]), ([a-zA-Z]) is the first capturing group; ([0-9]) is the second capturing group.

Use a backreference to replace the pattern matched by a capturing group. In Perl, these groups are represented by the special variables $1, $2, $3, and so on. ($1 indicates text matched by the first parenthetical group.) In JMP, use a backslash followed by the group number (\1, \2, \3).

The following example includes a third argument that specifies the replacement text and backreferences.

Regex(
	"   Are you there, Alice?, asked Jerry.", // source
	" (here|there).+ (\w+).+(said|asked) (\w+)\.", // regular expression
"  I am \1, \4, replied \2." ); // optional format argument

" I am there, Jerry, replied Alice."

" I am \1,

Creates the text “I am”, a space, and then the first matched pattern, “there”.

\4,

Creates the text “Jerry” with the fourth matched pattern (\w+).

replied \2."

Creates the text “replied” and a space. Matches “Alice.” with the second matched pattern (\w+).

需要更多信息?有问题?从 JMP 用户社区得到解答 (community.jmp.com).