RegEx Whitespace Vs. Eclipse

I'm trying to make a regex to match a whitespace, and while I'm doing this:

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

      

I know this should work because I tried it with Regex Buddy and it says it does, but when I try to start it with Eclipse it marks an error saying it is not a valid escape sequence and it automatically add 2 '\' regex rendering is useless .... could anyone tell me what to do? I've used period instead of \ s so far, but I really need \ s ...

thank


Added:

Well I understand, but although " \s

" is used for any space 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 continue even if it doesn't match ... = / or I got nothing?

0


source to share


2 answers


Exactly what do you mean when using regex in Eclipse? Are you trying to search and replace in the IDE, or are you trying to use regex in your Java code?

If you want to use regex in Java code, generate a Java source snippet under the Use tab in RegexBuddy and paste it into your code.

If you want the regex to be ready to be inserted into Java code, select Java Flavor from 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, fully formatted as a Java string. Your regex will be copied as:



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

      

The extra backslash is needed when using a Java string literal to store your regex.

PS: You can use \ s instead of [\ s]. Stores two keystrokes.

+1


source


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

Here's some Java code to 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 need to put a space "token" (\ s) in the character set ([]).

you are right, \ s are spaces.



Regex buddy needs input as the regex is actually written, the literal string expression in eclipse / source code needs to be escaped. If you read the template from a properties file, you don't need to avoid 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 doesn't work

I am assuming you mean eclipse where it fails? Just add these lines to the test, maybe it explains it a little 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());

      

+3


source







All Articles