引用

Understanding Backreferences
Text
This is a block of of text,

several words here are are

repeated, and and they

should not be.

RegEx
[ ]+(/w+)[ ]+/1

Result
This is a block of of


 text,

several words here are are



repeated, and and

 they

should not be.
Analysis
 

The pattern apparently worked, but how did it work? [ ]+ matches one or more spaces,

/w+ matches one or more alphanumeric characters, and [ ]+ then matches any trailing spaces.

But notice that /w+ is enclosed within parentheses, making it a subexpression.

This subexpression is not used for repeating matches; there is no repeat matching here.

Rather, the subexpression is used simply to group an expression,

to flag it and identify it for future use. The final part of this pattern is /1 ;

this is a reference back to the subexpression, and so when (/w+) matched the word of , so did /1 ,

and when (/w+) matched the word and , so did /1 .

 

例子二:结合引用进行替代(使用“$”)

Text
313-555-1234

248-555-9999

810-555-9000

RegEx
(/d{3})(-)(/d{3})(-)(/d{4})

Replace
($1) $3-$5

Result
(313) 555-1234

(248) 555-9999

(810) 555-9000
 
Analysis
 

Again, two regular expression patterns are used here. The first looks far more complicated than it is, so let's walk through it.

(/d{3})(-)(/d{3})(-)(/d{4}) matches a phone number, but breaks it into five subexpressions (so as to isolate its parts).

(/d{3}) matches the first three digits as the first subexpression, (-) matches as the second subexpression, and so on.

The end result is that the phone number is broken into five parts (each part its own subexpression): the area code, a hyphen,

the first three digits of the number, another hyphen, and then the final four digits. These five parts can be used individually and as needed,

and so ($1) $3-$5 simply reformats the number using only three of the subexpressions and ignoring the other two,

thereby turning 313-555-1234 into (313) 555-1234 .

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值