0

我有一个长字符串,其中包含如下信息:

|infoId-*-info||infoId-*-info||infoId-*-info| ...

我使用'-*-'字符作为分隔符,我想在字符的右侧进行搜索,但是如果匹配任何内容,'-*-'它应该返回整个部分,有没有办法做到这一点?infoId-*-infoinfo

提前致谢

例子:

这是我的字符串:

|8d9f9a0g9r8ad8f-*-sedat||0sdf7a89s9d0wg-*-derya||9g7a6w6e7d89awwe-*-caner|

对于这个字符串,如果我的搜索值是"edat"我如何返回整个8d9f9a0g9r8ad8f-*-sedat字符串,并且如果我的搜索值是"s"它也不应该返回,因为包含字符0sdf7a89s9d0wg-*-derya的左侧。如果你能给我支持这个例子的jsfiddle链接,那将非常受欢迎,非常感谢你花时间。"-*-""s"

4

2 回答 2

1

听起来你需要一些类似的东西

/((?:infoId)\/YOUR_REGEX_FOR_INFO)\s/

(我们不知道您的infoIdinfo结构如何)。

按照 OP 添加的示例进行编辑

您可以使用(对于edat):

/\|([^-]+-\*-[^|]*edat[^|]*\|/

我不知道您使用的是哪种语言,但您可以使用“全局”标志一次获取字符串中的所有匹配项。如果是 JS,它将是/\|([^-]+-\*-[^|]*edat[^|]*\|/g.

于 2012-04-19T11:16:55.020 回答
0

尝试这个:

(?s)(?<=/)[^/]+(?=/|$)

解释:

<!--
(?s)(?<=/)[^/]+(?=/|$)

Options: ^ and $ match at line breaks

Match the remainder of the regex with the options: dot matches newline (s) «(?s)»
Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=/)»
   Match the character “/” literally «/»
Match any character that is NOT a “/” «[^/]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=/|$)»
   Match either the regular expression below (attempting the next alternative only if this one fails) «/»
      Match the character “/” literally «/»
   Or match regular expression number 2 below (the entire group fails if this one fails to match) «$»
      Assert position at the end of a line (at the end of the string or before a line break character) «$»
-->
于 2012-04-19T11:16:56.090 回答