正则表达式 空白 VS. 蚀

发布于 2024-07-11 03:53:59 字数 473 浏览 7 评论 0原文

我正在尝试创建一个正则表达式来匹配空格,到目前为止我正在这样做:

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

我知道它应该可以工作,因为我已经用 Regex Buddy 尝试过它,它说它可以工作,但是当我尝试使用 Eclipse 运行它时它标记了一个错误,表示它不是有效的转义序列,并且它会自动添加 2 '\' 使正则表达式无用......有人可以告诉我该怎么做吗? 到目前为止,我最终使用了一个点而不是 \s,但我真正需要的是 \s...

谢谢


添加

嗯,我明白,但是,我虽然 '\s' 用于任何空白字符,正如我所说,Regex Buddy 也能识别它,如果我使用 '\s' 它不会在 Regex Buddy 中识别,因此测试失败,但在 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? I've so far ended up using a point instead of \s but what I really need is the \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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-07-18 03:53:59

'\'在java中标识转义字符,所以如果你想单独使用它,你需要转义它。 (\t 是制表符,\n 是换行符等)
要转义它,您需要添加 1 个“\”而不是 2 个。

下面是一些可以实现此目的的 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());
    }
}

您不必将空格“标记”(\s) 放入字符集中( [])。

你是对的,\s 是任何空格。

正则表达式伙伴需要输入,因为正则表达式实际上是编写的,eclipse/源代码中的文字字符串表达式需要转义。
如果您要从属性文件中读取模式,则无需转义它。

正如我所说,Regex Buddy 也认识到
它本身就是这样,如果我使用 \s 它不是
在 Regex Buddy 中得到认可,因此
测试失败

我假设你的意思是 Eclipse 失败的地方?
只需将这些行添加到测试中,也许这可以解释得更多:

        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());

A '\' identifies an escape character in java, so you need to escape it if you want to use it on it's own. (\t is tab, \n is newline etc.)
To escape it, you need to add 1 '\' instead of 2.

Here is some java code for you that will do the trick:

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 ([]).

you are correct, \s is any whitespace.

Regex buddy needs an input as the regular expression is actually written, a literal string expression in eclipse/source code needs to be escaped.
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

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());
一个人的旅程 2024-07-18 03:53:59

在 Eclipse 中使用正则表达式到底是什么意思? 您是否尝试在 IDE 中进行搜索和替换,或者尝试在 Java 代码中使用正则表达式?

如果您想在 Java 代码中使用正则表达式,请在 RegexBuddy 的“使用”选项卡上生成 Java 源代码片段,并将其粘贴到您的代码中。

如果您想单独将正则表达式粘贴到 Java 代码中,请在 RegexBuddy 顶部工具栏中选择 Java 风格。 然后单击“复制”按钮,并选择“将正则表达式复制为 Java 字符串”。 然后,RegexBuddy 会将您的正则表达式复制到正确格式化为 Java 字符串的剪贴板。 您的正则表达式将被复制为:

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

使用 Java 字符串文字存储正则表达式时,额外的反斜杠至关重要。

PS:您可以使用\s代替[\s]。 节省两次击键。

Exactly what do you mean with using a regular expression in 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?

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.

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. Then click the Copy button, and select Copy Regex as Java String. RegexBuddy will then copy your regex to the clipboard propertly formatted as a Java string. 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.

P.S.: You can use \s instead of [\s]. Saves two keystrokes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文