objective c - NSRegularExpression find pattern with optional part -
here thing :
i have file storing datas, following pattern :
item1:value1 item2:value2 item3:value3 // \n item1:value1 item2:value2 item1:value1 item2:value2 // , on... // item3:value3 optional
then store datas of file in nsstring
, deal them.
i want match value2 thing pesence of item3:value3
optional in each line.
so tried use ?
regular expression operator i'm not sure way use it.
so typically tried match following pattern (which doesn't work, ofc):
@"item1:.* item2:(.*) (item3:.*)?\n"
better explained, want regroup 2 conditions in 1 :
@"item1:.* item2:(.*) item3:.*\n" // case 1 : item3:.* present in line @"item1:.* item2:(.*)\n" // case 2 : item3 not present
note made personnal function returns matches in nsmutablearray
.
i hope clear enough :/
thanks , ideas.
ok, looks there couple of errors in regular expression: i'll run through them now.
firstly, trying match end of line "\n". work fine if string ends in new line, not match last line otherwise. fix this, use "$" symbol, , make sure pass nsregularexpressionanchorsmatchlines
options:
parameter when instantiate regular expression, like:
nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:@"item1:.* item2:(.*?)(?: item3:.*)?$" options:nsregularexpressionanchorsmatchlines error:nil];
the $
symbol called anchor, , default matches end of string. opposite ^
anchor, matches start of string. if pass nsregularexpressionanchorsmatchlines
option, however, these anchors change behaviour match start , end of line of string.
secondly, you're using plane parethensis, ()
, group "item3:" part, don't want group out result of match (a "capture"). if don't want "capture" text in group, write group (?:...)
. strictly, using plane parenthesis work (and in example), means regular expression engine must more work, needs keep track of what's inside capture group can access when method returns (in case rangeatindex:2
).
thirdly, misplaced space in regular expression (just before open parenthesis of item3 group), such regular expression match line if data of item2 ended in space or line had item3 entry. made seem though ?
wasn't working in regular expression, , have solved main problem on own. space needs inside group followed question mark, otherwise regular expression match if space there!
and finally: *
operator greedy default, meaning match as possibly can. has effect of making (.*)
part of regular expression eat of text until end of line, , regular expression still match, because (item3:.*)?
part optional. placing ?
after *
(i.e. .*?
) changes way *
works, making lazy matches little text possible, meaning that, if possible, regular expression prefer match item3 part of line (item3:.*)?
part of regular expression on item2:(.*)
part of regular expression.
so regular expression like:
@"item1:.* item2:(.*?)(?: item3:.*)?$"
Comments
Post a Comment