-1

I am incorporating a pattern with has a backslash(\) with an escape sequence once.But that is not working at all.I am getting result as no match.

package com.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestClassRegex {
    private static final String VALIDATION = "^[0-9\\-]+$";
    public static void main(String[] args) {
        String line = "1234\56";
        Pattern r = Pattern.compile(VALIDATION);
        Matcher m = r.matcher(line);
        if (m.matches()) {
         System.out.println("match");
        }
        else {
            System.out.println("no match !!");
        }

    }
}

How can I write a pattern which can recognize backslash literally.

I have actually seen another post : Java regular expression value.split("\\."), "the back slash dot" divides by character? which doesn't answer my question completely.Hence needs some heads up here.

Community
  • 1
  • 1
Ajay
  • 421
  • 3
  • 17
  • 4
    I think you need four total backslashes to get what you want. `"^[0-9\\\\-]+$"` – markspace Jun 26 '16 at 06:05
  • You probably need `String line = "1234\\56";`. But it would be helpful if you told us what your code does - does it compile? What does it print? – Duncan Jones Jun 26 '16 at 06:09
  • @Duncan, I really don't want to modify source of data. – Ajay Jun 26 '16 at 06:23
  • @markspace, is there a simple and more accurate way to do it. – Ajay Jun 26 '16 at 06:23
  • 1
    A string created using the string literal `"1234\56"` does not contain a backslash; `\5` is an octal escape sequence for `U+0005`, a control character. I assume your real string is coming from somewhere else, and really has a backslash in it, but the literal your test code should contain two backslashes, as @Duncan said. And the string literal representing the regex should have four backslashes, as @markspace said. – Alan Moore Jun 26 '16 at 06:32
  • Ducan didn't intended to change your data, but in Java, this `"1234\56" means "123456" because you espace the character '5' which gives '5'. So to have a backslash in a variable, you need to double it, as he said. Oups, just saw the comment of Alan Moore, which already explains it. – Master DJon Jun 26 '16 at 06:54
  • 1
    Also, that other question you linked to doesn't relate to your problem. Here;s a better one: http://stackoverflow.com/questions/2242417/how-to-remove-the-backslash-in-string-using-regex-in-java/2242486#2242486 – Alan Moore Jun 26 '16 at 06:56
  • Doh! It's actually `\56` (`U+002E`, or `.`), as Yassin points out below. But the takeaway is the same: your regex isn't trying to match a backslash, and your test string doesn't contain any. – Alan Moore Jun 26 '16 at 07:24

1 Answers1

1

"1234\56" will not produce "123456" but instead "1234."

Why?

The \ in a String is used to refer to the octal value of a character in the ASCII table. Here, you're calling \056 which is the character number 46 in the ASCII table and is represented by .

enter image description here

That's exactly the reason why you're not getting a match here.


Solution

You should first of all change your regex to ^[0-9\\\\-]+$ because in Java you need to escape the \ in a String. Even if your initial RegEx does not do it.

Your input needs to look like 1234\\56 for the same reason as above.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89