views:

1500

answers:

5

Hello, 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?

+3  A: 

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());
Raymond Roestenburg
A: 

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.

Jan Goyvaerts
A: 

As Jan already asked: what exactly are you doing in Eclipse? And on what text are you doing this? Perhaps there's a (or more) new line character in your target String. By default, the DOT operator does not match new line characters: maybe that's why Eclipse isn't matching anything.

But it's all pretty much guessing. For a better answer, you should provide a bit more detail.

Bart Kiers
I am doing a crawler in Eclipse, and the purpose of this regex is to match websites of this particular type of Forum. So the regex is now fine, but for some reason, when I run the program Eclipse can't finish running it...thank you =)
Luis Armando
A: 

hi im roldan rivas cabato . . .

roldan cabato
rolda rivas cabato from bangan,bot,zam. . .sept,28,1990 is my b-day. . .for me java is very difficult for programming langauge
roldan cabato
A: 

hi im roldan rivas cabato . . .

roldan cabato