简体   繁体   English

除某些字符外的所有字符串的正则表达式

[英]Regex For All String Except Certain Characters

I am trying to write a regular expression that matches a certain word that is not preceded by 2 dashes (--) or a slash and a star (/*). 我正在尝试编写一个正则表达式,该正则表达式与一个不带2个破折号(-)或斜杠和星形(/ *)的单词匹配。 I tried some expression but none seem to work. 我尝试了一些表情,但似乎没有任何作用。

Below is the text I am testing on 以下是我正在测试的文字

a_func(some_param); a_func(some_param);

/* a comment initialization */ / *注释初始化* /

init; 在里面;

I am trying to write a regex that will only match the word init in the last line alone, what I've tried so far is matching the word init in initialization and the last line, I tried to look for existing answers, and found that used negative lookahead, but it was still matching init in initialization . 我正在尝试编写一个仅与最后一行仅匹配init的正则表达式,到目前为止,我尝试在initialization和最后一行与init匹配的正则表达式,我试图寻找现有的答案,发现使用否定的前瞻,但在initialization时仍与init相匹配。 Below are the expressions I tried: 以下是我尝试过的表达式:

  • (?!\\/\\*).*(init)
  • [^(\\-\\-|\\/\\*)].*(init)
  • (?<!\\/\\*).*(init) While reading in regex101's quick reference, I found this negative lookbehind which I believe had a similar example to what I need but I was still not able to get what I want, should I look into the negative lookbehind more or is this not how I achieve what I want? (?<!\\/\\*).*(init)在regex101的快速参考中阅读时,我发现了这种负面的印象,我认为这与我需要的例子相似,但是我仍然无法获得我想要的东西,应该我更多地关注背后的负面因素,或者这不是我要达到的目标的方式吗?

My knowledge in regular expression is not that extensive, so I don't know if it is possible for what I want or not, but is it doable? 我对正则表达式的了解并不广泛,所以我不知道是否可以满足我的需求,但这可行吗?

Assuming the -- or /* are on the same line as the init , there are some options. 假设--/*init在同一行,则有一些选项。 As the commenters said, multiline comments will likely require stronger techniques. 正如评论者所说,多行评论可能需要更强大的技术。

The simplest way I know is to actually preprocess the strings to remove the --.*$ and /\\*.*$ , then look for init (or init\\b if you don't want to match initialization ): 我知道的最简单的方法是实际对字符串进行预处理以删除--.*$/\\*.*$ ,然后查找init (如果不想与initialization进行init ,则查找init\\b ):

String input = "if init then foo";
String checkme = input.replaceAll("--.*$", "").replaceAll("/\\*.*$", "");
Pattern pattern = Pattern.compile("init");  // or "init\\b"
Matcher matcher = pattern.matcher(checkme);
System.out.println(matcher.find());

You can also use negative lookbehind as in @olsli's answer. 您也可以在@olsli的答案中使用负向后看。

You can start with: 您可以从以下内容开始:

String input = "/*init";
Pattern pattern = Pattern.compile("^((?!--|\\/\\*).)*init");
Matcher matcher = pattern.matcher(input);
System.out.println(matcher.find());

I have added more braces to separate things out. 我添加了更多花括号以将内容分开。 This should also work, tested it in Regexr and IDEONE 这也应该工作,在Regexr和IDEONE中对其进行测试

Pattern p = Pattern.compile("^(?!=((\\/\\*)|(--)))([.]*?)init[.]*$", Pattern.MULTILINE|Pattern.CASE_INSENSITIVE);
String s = "/* Initialisation";
Matcher m = p.matcher(s);

m.find(); /* should return you >-1 if there's a match

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

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