在某个字符出现 n 次后选择的正则表达式

发布于 2024-12-07 04:02:30 字数 488 浏览 1 评论 0原文

在这里创建一些正则表达式。我想知道是否可以获得一些关于如何在一个字符出现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰雪之触 2024-12-14 04:02:30

您的正则表达式将如下所示:

/^(?:.+?\|){2}(.+?[^(Co)]\.)/

PHP

<?php
    preg_match('/^(?:.+?\|){2}(.+?[^(Co)]\.)/','xyz|yui|This is a Co. sentence. Ending before this clause.',$out);
    echo $out[1];
?>

HOWEVER

您应该通过管道字符进行分解并访问相应的信息,如下所示:

$stuff = explode('|','xyz|yui|i want to select this.');
echo $stuff[2];

Your regex would look like this:

/^(?:.+?\|){2}(.+?[^(Co)]\.)/

PHP

<?php
    preg_match('/^(?:.+?\|){2}(.+?[^(Co)]\.)/','xyz|yui|This is a Co. sentence. Ending before this clause.',$out);
    echo $out[1];
?>

HOWEVER

You should explode by the pipe character and access the respective information like that:

$stuff = explode('|','xyz|yui|i want to select this.');
echo $stuff[2];
十级心震 2024-12-14 04:02:30

首先,您需要创建一个包含重复部分 ([^|]+\|) 的组,可以将其设置为恰好出现两次 {2},那么你需要匹配其余的 (.*):

^([^|]+\|){2}(.*?)\.

Update

你可以将其取消分组,就像 ?: 提到的 @Karolis 一样,

^(?:[^|]+\|){2}(.*?)\.

第一个正则表达式第二场比赛将是你的,第二场比赛将是你的第一个。

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 (.*):

^([^|]+\|){2}(.*?)\.

Update

You can ungroup it as @Karolis mentioned with ?:

^(?:[^|]+\|){2}(.*?)\.

WIth the first regexp the second match will be yours, with the second it will be the first.

怂人 2024-12-14 04:02:30

这样就可以了:

$text = 'xyz|yui|i want to select Co. this. But not this.';
$re = '/# Match stuff after nth occurance of a char.
    ^               # Anchor to start of string.
    (?:[^|]*\|){2}  # Everything up through 2nd "|"
    (               # $1: Capture up through next "."
      [^.]*         # Zero or more non-dot.
      (?:           # Allow dot if in "Co.".
        (?<=Co)     # If dot is preceded by "Co",
        \.          # then allow this dot.
        [^.]*       # Zero or more non-dot.
      )*            # Zero or more "Co." dots allowed.
      \.            # First dot that is not "Co."
    )               # End $1: Capture up through next "."
    /ix';
$text = preg_replace($re, '$0|', $text);
echo $text;

编辑 2011-09-28 10:00 MDT:添加了跳过点的功能:“Co.”
编辑2011-09-28 10:30 MDT:更改为使用preg_replace()插入 |点后。

This will do it:

$text = 'xyz|yui|i want to select Co. this. But not this.';
$re = '/# Match stuff after nth occurance of a char.
    ^               # Anchor to start of string.
    (?:[^|]*\|){2}  # Everything up through 2nd "|"
    (               # $1: Capture up through next "."
      [^.]*         # Zero or more non-dot.
      (?:           # Allow dot if in "Co.".
        (?<=Co)     # If dot is preceded by "Co",
        \.          # then allow this dot.
        [^.]*       # Zero or more non-dot.
      )*            # Zero or more "Co." dots allowed.
      \.            # First dot that is not "Co."
    )               # End $1: Capture up through next "."
    /ix';
$text = preg_replace($re, '$0|', $text);
echo $text;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文