badc0re
badc0re

Reputation: 3523

adding regexp to yaml python

Is there any way to store and read this regexp in YAML by using python:

regular: /<title [^>]*lang=("|')wo("|')>/ 

Anyone have any idea or some solution for this ?

I have the following error:

    % ch.encode('utf-8'), self.get_mark())
yaml.scanner.ScannerError: while scanning for the next token
found character '|' that cannot start any token
  in "test.yaml", line 10, column 49

My code:

def test2():
    clueAppconf = open('test.yaml')
    clueContext = yaml.load(clueAppconf) 
    print clueContext['webApp']

Upvotes: 1

Views: 1896

Answers (1)

Jonathanb
Jonathanb

Reputation: 1264

Ok, it looks like the problem is the type of scalar you have chosen to represent this regex. If you're married to scalars (yaml strings), you'll need to use double quoted scalars and escape codes for your special characters that it chokes on. So, your yaml should look something like this:

regular: "/<title [^>]*lang=("\x7C')wo("\x7C')>/" 

I've only escaped the character that it was choking on to maintain some semblance of readability, however you may need to escape additional ones depending on whether it throws more errors. Additionally, you could use unicode escape codes. That would look like this:

regular: "/<title [^>]*lang=("\u007C')wo("\u007C')>/"

I'm a little out on my yaml knowledge, so I don't know a way to maintain the special characters and their readability in the yaml. Based on my cursory scan of the yaml documentation, this was the best I could find.

Upvotes: 2

Related Questions