Regex: Limiting Scope without capturing results

Gordon Chapman megalith at btinternet.com
Thu Jul 24 15:46:24 EDT 2003


Right, this has had me scratching my head for a while.

I want to limit the scope of an OR without using ()s as I don't want
to capture that specific result, I'm trying to split a multistatement
line up into a series of single statement lines, the multi statement
line is of the format;

Surface "/work/master/shaders/surface/surface_s07b" "color
VAR_EYE_COL1_TINT" [ 0.239215686275 0.2 0.2 ] "color
VAR_EYE_COL2_TINT" [ 0.4 0.301960784314 0.25 ] "color
VAR_EYE_COL3_TINT" [ 0.749019607843 0.749019607843 0.701960784314 ]
"color VAR_TEX_TINT" [ 0.0 0.0 0.0 ] "color diff_shadow_colour" [ 0 0
0 ] "color diff_tint" [ 1 1 1 ] "color env_sea_colour" [ 0.03 0.08
0.02 ] "color rim_shadow_colour" [ 0 0 0 ] "color rim_tint" [ 1 1 1 ]
"color spec_shadow_colour" [ 0 0 0 ] "color specmap_filter" [ 1 1 1 ]
"color specmap_shadow_colour" [ 0 0 0 ] "color surface_colour" [ 1 1 1
] ....

Now I have a pattern that kinda works fine when I use p.split with
capture parentheses, the first match is the complete string, but then
it gives me substring matches like "color", then the param name, then
the value, when all I want is the first string;

p = re.compile('\s+(\"(string|float|color)\s(\S+)\"\s\[\s*([0-9.
]+|\"\S*\")\s+\])')

Obviously I can just ignore the extraneous information, but I'd rather
come up with a neater way to do it (taking the easy way out also
rather defeats the purpose of this as an academic exercise :)

If I remove the ()s around the string|float|color match it stops
matching anything, so is there an alternative to these ? Or a way of
telling them to limit the scope of the match without capturing the
result ?

Thanks.

G.




More information about the Python-list mailing list