Message Boards Message Boards

0
|
2948 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

RegularExpression parsing problem

StringCases["700x35",
  RegularExpression["(\\d+(?:\\.\\d*)?)(/|x|X)(\\d{1,3}(?:\\.\\d*)?)(\\s*([a-zA-Z])?(R|D|B|r|d|b)?)[\\s*-]?(\\d+(?:\\.\\d*)?)"] :>
  {"$1", "$2", "$3", "$4","$5", "$6", "$7"}]

Out[1]= {{"700", "x", "3", "", "", "", "5"}}

I expected position "$3" to be 35. Instead, the 3 and 5 get split apart drastically. Any ideas how to tweak the above to keep 35 together?

I assumed that (\\d{1,3}(?:\\.\\d*)?) would take 1 to 3 digits followed by an optional decimal point and more digits, but this doesn't seem to be doing that in this case.

Thanks,
-Jeff
POSTED BY: Jeffrey Bryant
Well, I found that changing the RegEx to use:

In[66]:= StringCases["700x35",
RegularExpression[
"(\\d+(?:\\.\\d*)?)(/|x|X)(\\d{1,3}+(?:\\.\\d*)?)(\\s*([a-zA-Z])?(R|D|B|r|d|b)?)[\\s*-]?(\\d+(?:\\.\\d*)?)"] :> {"$1", "$2", "$3", "$4",
"$5", "$6", "$7"}]

Out[66]= {}

Prevents the bad parse. It doesn't bring the 3 and 5 together as I originally stated it should, but it does prevent the bad result which is fine for my purposes.
POSTED BY: Jeffrey Bryant
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract