Hey, I should do this more often! While writing the previous item I thought it would be nice to format the regular expression a bit by indenting it and adding some comment, this was the result:
# gobble any spaces
(\s*)
# main group
(?:
# keyword - gather letters until first space or =
([^=\s]*)
# option group
(?:
# option 1 - attribute has no quotes
(?:
# gobble spaces and =
(\s*?=\s*?)
# value - gather anything that's not a space or quote
([^\s"]+)
)
|
# option 2 - attribute has quotes
(?:
# gobble spaces and =
(\s*?=\s*?)
# value - gather anything between two quotes
(".*?")
)
)?
)
# gobble any spaces
(\s*)
# keyword - gather letters until first space or =
([^=\s]*)
# optional group containing the value
(?:
# gobble spaces and =
(\s*?=\s*?)
# option group
(?:
# value - (option 1) gather anything that's not a space or quote
([^\s"]+)
|
# value - (option 2) gather anything between two quotes
(".*?")
)
)?
(\s*)([^=\s]*)(?:(?:(\s*?=\s*?)([^\s"]+))|(?:(\s*?=\s*?)(".*?")))?
No comments:
Post a Comment