简体   繁体   English

检查单词前后的字符

[英]Check for characters before and after a word

I need to have a one positive regex which combines all 3 of the following: 我需要有一个积极的正则表达式,它结合了以下所有3:

-- ^(.*)\.abc\.(.*)$
-- ^(.*)\.abc$
-- ^abc\.(.*)$

such that the below cases do not apply to the regex: 这样以下情况不适用于正则表达式:

-- 123abc
-- .abc123
-- 123abc.

and the following fgive the positive result: 以下是正面结果:

--.abc
--.abc.
--abc.

I know that the 3 conditions can be in ORed in a group. 我知道这3个条件可以在一组中进行或运算。 I am looking fr a better solutoin, probably using lookaheads. 我正在寻找更好的溶剂,可能使用了先行剂。

I tried using backreferencing : /^.*([\\.]*)abc\\1.*$/ but then this is positive for negative cases as well. 我尝试使用反向引用: /^.*([\\.]*)abc\\1.*$/但是对于负面情况也是如此。

You can use this regex: 您可以使用此正则表达式:

^(.*([.;]))?abc((?=[.;])\2.*)?$

RegEx Demo 正则演示

RegEx Breakup: 正则表达式分解:

  • ^ : Start ^ :开始
  • (.*([.;]))? : start with anything followed by dot or semicolon (optional match). :以任何后跟点或分号(可选的匹配项)开头。 Note that we capture before abc in capture group #2 请注意,我们在捕获组2中的abc之前捕获
  • abc : match abc abc :匹配abc
  • ((?=[.;])\\2.*)? : End with same character we captured before abc and anything following (optional match). :结尾处是我们在abc之前捕获的相同字符,以及其后的所有字符(可选匹配项)。 Lookahead assertion is used to make sure we don't match empty back-reference \\2 前瞻性断言用于确保我们不匹配空的反向引用\\2
  • $ : End $ :结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM