# String pattern matching help with regular expressions

**URL:** <https://community.wolfram.com/t/string-pattern-matching-help-with-regular-expressions/143>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [January 30, 2013, 8:26pm UTC](https://community.wolfram.com/t/string-pattern-matching-help-with-regular-expressions/143 "2013-01-30T20:26:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jeffb/32/36128_2.png) [@jeffb](https://community.wolfram.com/u/jeffb)\
**Post date:** [January 30, 2013, 8:26pm UTC](https://community.wolfram.com/t/string-pattern-matching-help-with-regular-expressions/143/1 "2013-01-30T20:26:42Z")

</div>

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:

```auto
In[]:=
StringCases[StringSplit["P225/50R16.0 89S", "/"][[2]],
RegularExpression["(\\d+)(R|D|B )(\\d+.?\\d*)(\\s\\d+\\D)"] :>
"$4"][[1]]
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?

```auto
In[]:=
StringCases[StringSplit["P225/50R16.0 (89S)", "/"][[2]],
RegularExpression[
"(\\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]]

```

---

<div class="post-metadata">

**Author:** ![rolfm](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/rolfm/32/35274_2.png) [@rolfm](https://community.wolfram.com/u/rolfm)\
**Post date:** [January 30, 2013, 11:03pm UTC](https://community.wolfram.com/t/string-pattern-matching-help-with-regular-expressions/143/2 "2013-01-30T23:03:09Z")

</div>

And you don’t want to do a simple **StringReplace[“P225/50R16.0 (89S)”, {“(”-\>“”,“)”-\>“”}]** first ?

---

<div class="post-metadata">

**Author:** ![jmichelson](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/jmichelson/32/42227_2.png) [@jmichelson](https://community.wolfram.com/u/jmichelson)\
**Post date:** [January 31, 2013, 11:01pm UTC](https://community.wolfram.com/t/string-pattern-matching-help-with-regular-expressions/143/3 "2013-01-31T23:01:56Z")

</div>

Move the “\\s”:

```auto
In[]:= StringCases[StringSplit["P225/50R16.0 (89S)", "/"][[2]],
RegularExpression["(\\d+)(R|D|B )(\\d+.?\\d*)\\s(\\(?\\d+\\D\\)?)"] :> "$4"][[1]]
Out[]= "(89S)" 

```

---

<div class="post-metadata">

**Author:** ![schandler](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/schandler/32/38011_2.png) [@schandler](https://community.wolfram.com/u/schandler)\
**Post date:** [February 1, 2013, 3:35am UTC](https://community.wolfram.com/t/string-pattern-matching-help-with-regular-expressions/143/4 "2013-02-01T03:35:48Z")

</div>

Someone appears very interested in tires. Doesn’t this work?

```auto
StringCases["50R16.0 (89S)",
RegularExpression["(\\d+)(R|D|B )(\\d+.?\\d*)(\\s\\(?\\d+\\D\\)?)"] :> "$4"]

```
