Is there a way to write a regular expression pattern that will create one or two groups based on the input text. (i.e.)
// ONE
NSString *pattern = @""; ([0-9]+).([0-9]+)
NSString *inputText = @"ThisIs MyTest72.56String";
// OUTPUT match = 72.56, group1 = 72, group2 = 56
What I am trying to get is:
// TWO
NSString *pattern = @""; ([0-9]+).([0-9]+)
NSString *inputText = @"ThisIs MyTest72String";
// OUTPUT match = 72, group1 = 72, group2 = Empty
I was thinking I could use (?:) but that just removes the group
What I am after is:
Text = "ThisIs MyTest72String"
Match = 72
Group1 = 72
Group2 = Empty
Text = "ThisIs MyTest72.56String"
Match = 72.56
Group1 = 72
Group2 = 56
EDIT:
This sort of works, although I would like to get rid of the “S” in the initial match.
Pattern = ([0-9]+).([0-9]*)
Text = "ThisIs MyTest72String"
Match = 72S
Group1 = 72 //RangeAtIndex:1 {13,2}
Group2 = Empty //RangeAtIndex:2 {16,0}
Text = "ThisIs MyTest72.56String"
Match = 72.56
Group1 = 72
Group2 = 56
This is close, but in the case of “Empty” (Group2) I was expecting the rangeAtIndex:2 to equal NSNotFound. The docs say “The range {NSNotFound, 0} is returned if one of the capture groups did not participate in this particular match” does the group being empty not count as “Not participating”?
Does this give you what you want?
I’ve escaped the decimal place (which you hadn’t, unsure if this is needed in your target language) and grouped the decimal and everything after it as a optional non captured group.
Should just be a matter of checking for the existence of a second group.