Message Boards Message Boards

3 Replies
4 Total Likes
View groups...
Share this post:

String pattern matching help with regular expressions

I'm trying to write a string parser for a specific task and have made good progress, but one modification seems to cause problems I don't know how to solve. In the following, I need to pull out the " 89S" part and the following code does this:
StringCases[StringSplit["P225/50R16.0 89S", "/"][[2]],
RegularExpression["(\\d+)(R|D|B )(\\d+.?\\d*)(\\s\\d+\\D)"] :>
Out[]= " 89S"
But this last piece may or may not have parens around it. When I try to modify the above the allow for optional parens, it doesn't work. Any idea how to tweak the below to pull out the " (89S)" part?
StringCases[StringSplit["P225/50R16.0 (89S)", "/"][[2]],
"(\\d+)(R|D|B )(\\d+.?\\d*)(\\(?\\s\\d+\\D\\)?)"] :> "$4"][[1]]

During evaluation of In[]:= Part::partw: Part 1 of {} does not exist. >>
Out[]= {}[[1]]
POSTED BY: Jeffrey Bryant
3 Replies
Someone appears very interested in tires.  Doesn't this work?
StringCases["50R16.0 (89S)",
RegularExpression["(\\d+)(R|D|B )(\\d+.?\\d*)(\\s\\(?\\d+\\D\\)?)"] :> "$4"]
POSTED BY: Seth Chandler
Move the "\\s":
In[]:= StringCases[StringSplit["P225/50R16.0 (89S)", "/"][[2]],
RegularExpression[ "(\\d+)(R|D|B )(\\d+.?\\d*)\\s(\\(?\\d+\\D\\)?)"] :> "$4"][[1]]
Out[]= "(89S)"
POSTED BY: Jeremy Michelson
And you don't want to do a simple StringReplace["P225/50R16.0 (89S)", {"("->"",")"->""}] first ?
POSTED BY: Rolf Mertig
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract