python - With pyparsing, how do you parse a quoted string that ends with a backslash -


I am trying to use the following conditions to parse the string cited:

  • The quoted string can contain an internal quote.
  • I want to use backslash to avoid internal quotes.
  • The quoted string may end with a backslash.

    I am struggling to define a successful parser. Also, I wonder if the regular expressions used by pipersing for this type of quoted strings are correct (see my alternate regular expression below).

    Am I using the wrong (most likely) piping or there is a bug in peppersing?

    Here is a script that shows the problem ( Note: do not ignore this script; please focus instead of the update below. ):

     Importing as PP Import Importing PP # as a single-quoted string: # - Internal quote bid # - A backslash as the last character before the final quote. Txt = r "'ab' \ cd \ '" Parsing with pipercing # does not work as expected: only takes the first 3 characters Parser = pp.QuotedString (quoteChar = "'", escChar = '\\', EscQuote = '\\') toks = parser.parseString (txt) Print Print 'txt:', txt Print 'Pattern:', Parser Pattern print 'Tox:', with a reggix like tox # parse pipping pattern, but # flipped with the last two groups - which looks more suitable for me # This work Rgx = re.compile (r "\ '(?: [^' \ N \ r \\] | (?: \\.) | (?: \\)) * \ '") Print print RGX Search (Txt) .group (0)   

    Output:

      txt: 'ab' cd \ 'Pattern: \' (?: [^ ' N \ r \\ | | (?: \\) | (?: \\.)) * 'Toks: ["ab'"] 'ab' cd ''   

    Update

    Thanks for the answer I suspect that I have confused things by preparing my question badly, so I should try again.

    Suppose we are trying to parse a language that generally quotes the rules of Python. We want users to be able to define a string that can include internal quotes (protected by backslash) and we want to eliminate those strings with backslash. Here is an example file in our language. The file will also pars as valid Python syntax, and if we print foo (in Python), then the actual value of the output will be: ab'cd \

      # demo.txt foo = 'ab' cd \\ '  

    My goal is to use pipering to parse such a language Is there any way of doing it? The above question is basically where I ended up after many unsuccessful attempts. Below is my initial attempt, it fails because in the end there are two backslashes, instead of just one.

      Open ('demo.txt') as FH: txt = fh.read (). Split () [-1] .strip () parser = pp. Printed 'pattern:', parser Pattern print 'toks:', toks # [Quoted string = "" ", escChar = '\\') toks = parser.parseString (txt) Print Print 'txt:', "Ab'cd \\\\"]   

    I think the problem is that cited string only recognizes backslash with a quote-escape -Pathon with a backslash avoids more general purpose.

    Is there an easy way to do this that I am ignoring? After the fact that by handling double backslash To do this, use .setParseAction (...) - perhaps it's a work that looks like:

      qhandler = lambda s, l, t : [T [0] .replace ('\\\' ',' \\ ')] parser = pp. Quoted string (excerptor = "'", escChar = '\\') .setParseAction (qHandler)   

    What about this code that is not working for you?

    Alphas) ​​strValue = quotedString ("'", escChar =' \\ ') strAssign = ident +' = '+ strValue result = strAssign.parseString (s) Print result. Repr ff of each element in Therm displays results: Print R # displays the str form of each element # backslash backslash = '\\' print result [-1] .count (backslash)

    print: < / P>

      ['foo', '=', 'ab'cd \\\\ "] foo = ab'cd \\ 2   

    Edit:

    Then "\" just gets '', but "\" is parsed but remains "\" instead of avoiding "\". It seems like a bug in the quotation, for now you can add this solution:

      import strValue.setParseAction (Lambda T: re. Sub (r '\\ (.)' , R '\ g <1>, t [0]))   

    which will take the order of every escape characters and return only the letter left without " Will give.

    I will add it to the next patch release of patching.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -