lundi 28 mars 2016

Regular expression that matches "{$" AND NOT matches "\{$"

I am working on a project with lexical analysis and basically I have to generate tokens that are text and that are not text.

  • Tokens that are text are considered all characters until the "{$" sequence.
  • Tokens that are not text are considered all characters inside the "{$" and "$}" sequences.

Note that the "{$" character sequence can be escaped by writing "\{$" so this also becomes a part of text.
My job is to read a String of text, and for that I am using Regular expressions.

I am using the Java Scanner and Pattern classes and this is my work so far:

String text = "This is \\{$ just text$}\nThis is {$not_text$}."
Scanner sc = new Scanner(text);
Pattern textPattern = Pattern.compile("{\\$"); // insert working regex here
sc.useDelimiter(textPattern);

System.out.println(sc.next());

This is what should be printed out:

This is \{$ just text$}
This is

How do I make a regex for the following logical statement:

match "{$" AND NOT match "\{$"

Aucun commentaire:

Enregistrer un commentaire