0

I have an String Like that:

String word ="[word";

I want to search this string as a substring to find Strings that contains these string. I have a query like that :

String Quert ="PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
            + " SELECT ?class WHERE { ?class rdfs:label ?v ." +
            "FILTER regex(?v, \"" + word +"\", \"i\")}" ;

and when I run the program an error occur.

Regex: Pattern exception: java.util.regex.PatternSyntaxException: Unclosed character class near index 6

So, how can I search this substring without producing this error?

edit: I use

Pattern.quote(word)

but I is producing another error:

Lexical error at line 1, column 118.  Encountered: "Q" (81), after : "\"\\"
Mosafer Koochooloo
  • 189
  • 1
  • 1
  • 8
  • Your `word` has `[` and no `]`. If you want to match a literal `[word` you need to `quote` the pattern with `Pattern.quote(word)`, or encose with `\Q` and `\E`. See http://stackoverflow.com/questions/15409296/what-is-the-use-of-pattern-quote-method. – Wiktor Stribiżew Jan 31 '17 at 10:13
  • You have to escape the `[` since it's part of REGEX families. – UninformedUser Jan 31 '17 at 10:17

0 Answers0