-1

I'm having problems with Java RegEx. That's my regex statement "\"730\"\s+{([^}]+)}" and it works on an regex checking website, but I have trouble getting it to work in Java. That's my current code.

    String patternString = '\"730\"\s+{([^}]+)}';
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(vdfContentsString);
    boolean matches = matcher.matches();

Thanks for advice.

It says "Illegal escape character in character literal".

7
  • you are definitely getting compile time error? Commented Jul 14, 2014 at 21:45
  • @Braj It says "Illegal escape character in character literal". Commented Jul 14, 2014 at 21:48
  • @Braj that's the part I want to find: "730" { "installdir" "C:\\Program Files (x86)\\Steam\\steamapps\\common\\Counter-Strike Global Offensive" "HasAllLocalContent" "1" "UpToDate" "1" } Commented Jul 14, 2014 at 21:51
  • Now it says "Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 7 "730"\s+{([^}]+)}" Commented Jul 14, 2014 at 21:53
  • what is the value of vdfContentsString? Commented Jul 14, 2014 at 21:54

3 Answers 3

1

Single quotes (') declare characters, double quotes (") declare strings, that's why you get the syntax error Illegal escape character in character literal. Second, regex itself syntactically uses the backslash, as in \s for whitespace. Maybe confusing might be the fact that Java also uses \ for character escaping. That's why you need two backslashes (\\s in Java will become \s for the resulting regular expression).

Then you need to take care of special characters in regular expressions: { and } are quantifiers ("repeat n times"), if you want them literally, escape them (\\{ and \\})

So if you want to match a string like "730" {whatever}, use this regular expression:

 "730"\s+\{([^}]+)\}

or in Java:

 String patternString = "\"730\"\\s+\\{([^}]+)\\}";

Example:

    String str = "\"730\" { \"installdir\" \"C:\\Program Files (x86)\\Steam\\steamapps\\common\\Counter-Strike Global Offensive\" \"HasAllLocalContent\" \"1\" \"UpToDate\" \"1\" }";
    String patternString = "\"730\"\\s+\\{([^}]+)\\}";
    System.out.println(str.matches(patternString)); // true
Sign up to request clarification or add additional context in comments.

1 Comment

@user3347446 If you're reading from a text file, trim the string (String line = line.trim();) after reading a line to remove line breaks or add (?s).* or [\r\n]* to the regular expression above in order to match line breaks.
1

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition

Escape { and } as well because in Java Regex Pattern it has special meaning.

String patternString = "\"730\"\\s+\\{([^\\}]+)\\}";

EDIT

String#matches() method looks for whole string if you are looking for sub-string of a long string then use Matcher#find() method and get the result from the groups that is captured by enclosing the pattern inside parenthisis (...).

sample code:

String patternString = "(\"730\"\\s+\\{([^\\}]+)\\})";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(vdfContentsString);

while (matcher.find()) {
    System.out.println(matcher.group(1));
}

6 Comments

Now there are no matches when in theory it should find one
have a look at the sample code. Is this what are you doing?
that's exactly what I do
It works with that one, but when I use the real text file it doesn't anymore
The problem is with String.matches() that matches whole string not just a part of it. have a look at the update
|
0

{, } are Metacharacters (See HERE for metacharacters) and need to be escaped with \\, hence, \\{ .. \\}.

\ is an escape character, while \s, \w, \d etc (See HERE for a list) are metacharacters, therefore, as mentioned above, these need to be escaped as well, hence, \\s+

instead of [^\\}], i would suggest (.+?)}

This is working:

String patternString = '\\\"730\\\"\\s+\\{(.+?)\\}';

The above is the required Java string which gets parsed into the following regular expression: \"730\"\s+\{(.+?)\}, and then it can be used to match the input string. Tadan!
two levels of parsing!

2 Comments

It doesn't find matches, though, when in theory it should.
u're welcome! check this: fiddle.re/ew005 .. click Java and see how the regex is being linked to the java string and subsequently matched

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.