RegEx in Java not working as I expected -


Trying to remove the double wrapped wires. For example [[This is a token]] that must be matched. To make things even more elegant, the escape sequence should be so that the items with double brackets like \ [[the escape escaped from it]] do not match.

Pattern [^ \\\ \] ([\\ [] {2}. [^ \\\\] [\\]] {2}) token To remove is close to "group 1", but there are conditions that do not work. The problem is that the first "no" statement is being evaluated as "anything except a backslash", the problem is, If "nothing" does not include "nothing" then, is this pattern "nothing besides backslash or match any character"?

There is a unit test to show the desired behavior:

  import java util.regex.Matcher; Import java.util.regex.Pattern; Import janit Framework. Test Sees; Public class RegexSpike TestCase {Private string regex; Private pattern pattern; Private Maitre Matter; @ Override protected zero setup () throws exception {super.setUp (); Regex = "[^ \\\\] ([\\ [] {2}. [^ \\\\] [\\]] {2})"; Pattern = Pattern.compile (regex); } Private string rungages (string teststring) {michter = pattern.machter (teststring); Return matcher.find ()? Matcher.group (1): "Not Found"; } Public Zero TestBeginsWitTags_Pages () {assertEquals ("[[should work]]", runerges ("[[should work]]")}} Public Zero TestBuginWithSpace_Passis () {esacrects ("[[should work]]" RunerGox ("[[should work]]")}} Public Zero Testing special special procedures () {assertEquals ("[[should work]]", runerges ("Anything here [[should work]]"); } Public Zero Test Concerns Charge () {assertEquals ("[[should work]]", rung Ax ("[[working]] with anything here")} Public Zero Testing and Anxious Charses () {Attempt to emphasize ("[[should work]]", Runergesk ("Anything here [[should work Public Zero Testing Brackets_Fell () {} [Nothing to be found], rungages ("\ \ [[should not work]]")}} Public Zero Testing Firstbrackets Escap_file () {} Emphasizing properties ("not found", runerges ("[ Should not work] "));} Public Zero TestScacketsBacketsSpace_file () {Attempted to push (" not found ", rungages (" [[should not work \\]] "); }}  

You just use (^ | [^ \ \] , which will match the beginning of a string or (if you set the MULTILINE mode on your regex) or A single character that is not backslash (including spaces, newlines, etc.)

You will also have to change . + with . +? Because otherwise a string like "[[one]] and [[two]]" will be seen as a single match, where "one]] and [[two] " is considered between brackets.

The third thing is that you do not have to wrap a letter (even \ [ or ] ) [] In a class with .

By doing this, following reggae (I apologize for the double-aspiration removal for clarity):

  (^ | [^ \\]) (\ [{2 }. [^ \\] \] {2})  

(Also keep in mind that you can not escape escape characters, your Reggae a [ Two slashes will not be parsed as a single (escaped) slash before , but will indicate a single (unchanged) slash and a escape bracket.)


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -