开发者

Regular Expressions marked subexpression

Suppose I have a 10 letter word, say "HelloWorld". I want to recall 开发者_高级运维the first 5 characters ("Hello") separately and the complete 10 letters ("HelloWorld") separately within one single regular expression. Is there a way to do so by using a single RE only? I mean by using the "()" metacharacter.


Looking for:

/((.{5}).*)/

Though the first group will be the whole word, the second will be the partial.

Unless this is being broken down differently than an arbitrary number of characters


Here's a version using just the regular expression syntax supported by sed(1):

$ echo HelloWorld | sed 's/^\(.....\).*$/First: "\1" All: "&"/'
First: "Hello" All: "HelloWorld"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜