简体   繁体   English

如何在Eclipse中搜索Java字符串?

[英]How to search for a Java string in Eclipse?

I need to search for instances of a character sequence in a Java string using Eclipse. 我需要使用Eclipse在Java字符串中搜索字符序列的实例。 Is there an easy way or regex to do this? 有这么简单的方法或正则表达式吗?

Example: Search for Eggs should match 示例:搜索Eggs应该匹配

String str = "Eggs and toast."

and not match 并不匹配

Eggs e = new Eggs()
  • Under search menu (press ^H) 在搜索菜单下(按^ H)
  • Go to File Search tab 转到File Search选项卡
  • check Regular expression radio button 检查Regular expression单选按钮
  • Enter text ".*?Eggs[^"]*" in Containing Text field on top 在顶部Containing Text字段中输入文本".*?Eggs[^"]*"
  • Click on Search button 单击“搜索”按钮

这样的事情对你有用吗?

\".*Eggs.*\"

In general this is very hard because what you want to search for is not "regular" in the sense of "regular expression". 一般来说这很难,因为你想要搜索的内容在“正则表达式”意义上不是“常规”。

One of the answers you've had suggests: 您已经提出的答案之一建议:

\".*Eggs.*\"

which is pretty good, but will still match, for example, 这是相当不错的,但仍然会匹配,例如,

System.out.println("There are " + new Eggs().count() + " eggs");

In general, there is not going to be a regular expression which does exactly what you want. 一般来说,没有一个正则表达式可以完全满足您的需求。

Searching for delimited text using regex is dodgy, however this will basically hit your target (and not too many others): 使用正则表达式搜索分隔文本是狡猾的,但这基本上会击中你的目标(而不是太多其他人):

".*Eggs.*"

Use Search > File > with regex with exactly this search term (ie including the quotes) 使用搜索>文件>与正则表达式完全匹配此搜索词(即包括引号)

This will however also hit lines like this: 然而,这也会像这样:

 x = "foo" + new Eggs() + "bar";

Also note that you don't need to delimit the double quote, because it's just a literal as far as regex is concerned 另请注意,您不需要分隔双引号,因为就正则表达式而言,它只是一个字面值

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

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