Regex 用于匹配字符串但不包含子字符串的正则表达式
我正在使用一些JSF代码,需要在项目中的所有Regex 用于匹配字符串但不包含子字符串的正则表达式,regex,xml,Regex,Xml,我正在使用一些JSF代码,需要在项目中的所有中插入stlyClass。问题是有很多命令按钮,我不想手动执行。另一个问题是,有些commandButtons已经包含styleClass,而有些则没有。因此,我需要一个正则表达式来查找不包含styleClass的commandButton,这样我就可以先在那里插入代码,然后再编辑其余的代码。希望我的解释有道理。请尝试回答问题,而不是采取变通办法 非常感谢 假设您正在讨论Eclipse搜索和替换功能,下面的正则表达式应该适合您。在某些情况下,它会添加一
中插入stlyClass
。问题是有很多命令按钮,我不想手动执行。另一个问题是,有些commandButtons已经包含styleClass,而有些则没有。因此,我需要一个正则表达式来查找不包含styleClass
的commandButton,这样我就可以先在那里插入代码,然后再编辑其余的代码。希望我的解释有道理。请尝试回答问题,而不是采取变通办法
非常感谢 假设您正在讨论Eclipse搜索和替换功能,下面的正则表达式应该适合您。在某些情况下,它会添加一些“不必要的”空白,但只需让eclipse再次自动格式化整个文件,就可以删除这些空白。 (选择项目根目录并按CTRL+Shift+F选择最懒的选项,或者忽略空白。) 要验证它是否与所有命令按钮匹配,请执行以下操作:
- 在
I gather上运行文件搜索,您想在Eclipse中对XML风格的文件执行基于正则表达式的查找和替换吗?如果是这样,最好在你的问题过于含糊而获得更多的反对票之前告诉/确认。这至少不是JSF的问题。只要将JSF文件视为XML文件就足够了。正则表达式大师会更好地理解您;)这是查找/替换(如Ctrl-F)问题吗?非常感谢!!这真的让我的日子轻松多了:)问题解决了!我应该从什么着手逐步提高regex水平?@RashidTevez是一个很好的来源。重要的是,您不仅需要了解模式,还需要了解引擎是如何工作的。因此,即使您已经知道,
意味着什么-阅读引擎如何处理它,以完全掌握它在某些情况下的实际含义。(链接的网站在Regex引擎部分几乎可以查看所有内容)。除此之外:熟能生巧;-)比你强!我很感激*?
<h:commandButton([^>]+(?=styleClass)|)(?:styleClass="([^"]+)")?([^>]*?)(\/?>)
<h:commandButton$1 styleClass="myNewStyleClassForEveryCommandButton $2"$3$4
<h:commandButton /> <h:commandButton> </h:commandButton> <h:commandButton styleClass="something" /> <h:commandButton styleClass="something"> </h:commandButton> <h:commandButton attr="value" styleClass="something more" /> <h:commandButton attr="value" styleClass="something more"> </h:commandButton> <h:commandButton attr="value" styleClass="something" anotherAttr="somethingElse"/> <h:commandButton attr="value" styleClass="something" anotherAttr="somethingElse"> </h:commandButton>
<h:commandButton styleClass="myNewStyleClassForEveryCommandButton " /> <h:commandButton styleClass="myNewStyleClassForEveryCommandButton "> </h:commandButton> <h:commandButton styleClass="myNewStyleClassForEveryCommandButton something" /> <h:commandButton styleClass="myNewStyleClassForEveryCommandButton something"> </h:commandButton> <h:commandButton attr="value" styleClass="myNewStyleClassForEveryCommandButton something more" /> <h:commandButton attr="value" styleClass="myNewStyleClassForEveryCommandButton something more"> </h:commandButton> <h:commandButton attr="value" styleClass="myNewStyleClassForEveryCommandButton something" anotherAttr="somethingElse"/> <h:commandButton attr="value" styleClass="myNewStyleClassForEveryCommandButton something" anotherAttr="somethingElse"> </h:commandButton>