简体   繁体   English

正则表达式空白与。 日食

[英]RegEx Whitespace Vs. Eclipse

I´m trying to make a regular expression to match a whitespace and so far I´m doing this: 我试图做一个正则表达式来匹配空格,到目前为止,我正在这样做:

Powered[\s]*[bB]y.*MyBB

I know it should work because I've tried it with Regex Buddy and it says it does but when I try to run it with Eclipse it marks an error saying it's not a valid escape sequence and it automatically adds 2 ´\\´ rendering the regex useless....could someone tell me what to do? 我知道它应该工作,因为我已经用Regex Buddy尝试过了,它说可以,但是当我尝试用Eclipse运行它时,它指出了一个错误,说这不是有效的转义序列,它会自动添加2´\\´来渲染正则表达式没用...。有人可以告诉我该怎么做吗? I've so far ended up using a point instead of \\s but what I really need is the \\s... 到目前为止,我最终使用的是点而不是\\ s,但我真正需要的是\\ s ...

Thanks 谢谢


Added : 新增

Well, I understand but, I though ' \\s ' is used for any whitespace character and as I said, Regex Buddy also recognizes it as such, and if I use ' \\s ' it is not recognized in Regex Buddy hence the test fails, but in eclipse it allows me to go on, even though it matches nothing... =/ or did I not get something? 好吧,我明白了,但是,尽管我将' \\s '用于任何空格字符,并且正如我所说,Regex Buddy也可以识别它,如果我使用' \\s ',则在Regex Buddy中无法识别,因此测试失败,但是在月食中它让我继续前进,即使它什么都不匹配... = /还是没有得到任何东西?

A '\\' identifies an escape character in java, so you need to escape it if you want to use it on it's own. '\\'标识Java中的转义字符,因此,如果要单独使用它,则需要对其进行转义。 (\\t is tab, \\n is newline etc.) To escape it, you need to add 1 '\\' instead of 2. (\\ t是制表符,\\ n是换行符,等等。)要对其进行转义,您需要添加1'\\'而不是2。

Here is some java code for you that will do the trick: 这是一些可以帮助您解决问题的Java代码:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Assert;
import org.junit.Test;


public class RegexTest {
    @Test
    public void testPatternWithWhiteSpace(){
        Pattern pattern = Pattern.compile("Powered\\s*[bB]y.*MyBB");
        Matcher matcher = pattern.matcher("Powered     By     MyBB");
        Assert.assertTrue(matcher.matches());
        matcher = pattern.matcher("Powered_By     MyBB");
        Assert.assertFalse(matcher.matches());
    }
}

You don't have to put the whitespace 'token' (\\s) in a character set ([]). 您不必在字符集([])中放置空格“令牌”(\\ s)。

you are correct, \\s is any whitespace. 您是正确的,\\ s是任何空格。

Regex buddy needs an input as the regular expression is actually written, a literal string expression in eclipse/source code needs to be escaped. 正则表达式的伙伴需要输入,因为正则表达式实际上是编写的,因此必须转义eclipse /源代码中的文字字符串表达式。 If you would read the pattern from say a properties file, you would not need to escape it. 如果您从某个属性文件中读取该模式,则无需对其进行转义。

as I said, Regex Buddy also recognizes it as such, and if I use \\s it is not recognized in Regex Buddy hence the test fails 如我所说,Regex Buddy也可以识别它,如果我使用\\ s,则Regex Buddy无法识别它,因此测试失败

i'm assuming you mean eclipse there where it fails? 我假设您的意思是在失败的地方蚀? Just add these lines to the test, maybe that explains it a bit more: 只需将这些行添加到测试中,也许可以进一步解释一下:

        matcher = pattern.matcher("Powered\t\n\r By     MyBB");
        Assert.assertTrue(matcher.matches());
        matcher = pattern.matcher("Powered\t\n\r ,By     MyBB");
        Assert.assertFalse(matcher.matches());

Exactly what do you mean with using a regular expression in Eclipse? 在Eclipse中使用正则表达式到底意味着什么? Are you trying to do a search-and-replace in the IDE, or are you trying to use a regex in your Java code? 您是要在IDE中进行搜索和替换,还是要在Java代码中使用正则表达式?

If you want to use a regex in Java code, generate a Java source code snippet on the Use tab in RegexBuddy, and paste that into your code. 如果要在Java代码中使用正则表达式,请在RegexBuddy的“使用”选项卡上生成Java源代码片段,然后将其粘贴到您的代码中。

If you want to get the regex alone ready to paste into Java code, select the Java flavor in the toolbar at the top in RegexBuddy. 如果要单独准备将正则表达式粘贴到Java代码中,请在RegexBuddy顶部的工具栏中选择Java风格。 Then click the Copy button, and select Copy Regex as Java String. 然后单击“复制”按钮,然后选择“将正则表达式复制为Java字符串”。 RegexBuddy will then copy your regex to the clipboard propertly formatted as a Java string. 然后,RegexBuddy将把您的正则表达式复制到正确设置为Java字符串格式的剪贴板中。 Your regex will be copied as: 您的正则表达式将被复制为:

"Powered[\\s]*[bB]y.*MyBB"

The extra backslash is essential when using a Java string literal to store your regex. 使用Java字符串文字存储正则表达式时,额外的反斜杠必不可少。

PS: You can use \\s instead of [\\s]. PS:您可以使用\\ s代替[\\ s]。 Saves two keystrokes. 保存两次击键。

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

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