在某个字符出现 n 次后选择的正则表达式
在这里创建一些正则表达式。我想知道是否可以获得一些关于如何在一个字符出现 n 次之后和某个字符下一次出现之前选择字符串的指示。
例如
xyz|yui|i want to select this.
在这个例子中我想在第二个“|”之后选择并在下一个“.”之前。所以我想要匹配的文本是“我想选择这个”。
我很感激任何指点,谢谢。
更新
为了更具体地说明为什么我需要在上面执行此操作,“我想选择此”末尾的句点后有更多文本。基本上,这是我试图界定的无限内容。到目前为止,我已经能够分隔前两个字段,现在我需要能够仅选择最后一个“|”之后的文本并在下一个句点之前添加“|”性格到最后。因此,期望的结果很
xyz|yui|i want to select this.|
抱歉没有更具体地说明结果,我希望这能澄清一点。感谢您提供的信息,超级棒。
Creating some regex expressions here. I was wondering if I could get some pointers on how to go about selecting a string after n occurences of one character and before the next occurence of a certain char.
for instance
xyz|yui|i want to select this.
In this example I am wanting to select after the 2nd "|" and before the next ".". So the text I want to match is "i want to select this".
I appreciate any pointers thanks.
UPDATE
To be more specific on why I need to do this above, there is more text after the period at the end of "I want to select this.". Basically this is undelimited content which I am trying to delimit. Thusfar I have been able to delimt the first two fields, now I need to be able to select only text after the last "|" and before the next period and add a "|" character to the end. So the desired result would be
xyz|yui|i want to select this.|
Sorry for not being more specific on the outcome and I hope this clears it up a bit. Thanks for the info, its super.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的正则表达式将如下所示:
PHP
HOWEVER
您应该通过管道字符进行分解并访问相应的信息,如下所示:
Your regex would look like this:
PHP
HOWEVER
You should explode by the pipe character and access the respective information like that:
首先,您需要创建一个包含重复部分
([^|]+\|)
的组,可以将其设置为恰好出现两次{2}
,那么你需要匹配其余的(.*)
:Update
你可以将其取消分组,就像
?:
提到的 @Karolis 一样,第一个正则表达式第二场比赛将是你的,第二场比赛将是你的第一个。
First you need to create a group which contains the repeating part
([^|]+\|)
here, which can be set to appear exactly two times{2}
, then you need to match the rest(.*)
:Update
You can ungroup it as @Karolis mentioned with
?:
WIth the first regexp the second match will be yours, with the second it will be the first.
这样就可以了:
编辑 2011-09-28 10:00 MDT:添加了跳过点的功能:
“Co.”
编辑2011-09-28 10:30 MDT:更改为使用
preg_replace()
插入 |点后。This will do it:
Edit 2011-09-28 10:00 MDT: Added ability to skip over dots in:
"Co."
Edit 2011-09-28 10:30 MDT: Changed to use
preg_replace()
to insert | after dot.