# String matching with Cases

**URL:** <https://community.wolfram.com/t/string-matching-with-cases/190>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [April 23, 2013, 4:28pm UTC](https://community.wolfram.com/t/string-matching-with-cases/190 "2013-04-23T16:28:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zachb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/zachb/32/8769_2.png) [@zachb](https://community.wolfram.com/u/zachb)\
**Post date:** [April 23, 2013, 4:28pm UTC](https://community.wolfram.com/t/string-matching-with-cases/190/1 "2013-04-23T16:28:29Z")

</div>

I’m trying to get the following to return “pfam03571” from the XML structure.

```auto
Cases[ 
  XMLElement[ 
    "myelem", {}, {XMLElement[ 
    "INSDQualifier_value", {}, {"Peptidase family M49; pfam03571"}]}], 
    XMLElement[_, {}, {___~~x:RegularExpression["pfam\\d+"]~~___}]:>x, 
  Infinity] 

```

This doesn’t work. Is this possible without extracting all XMLElement values (I don’t know the element tag name) and using StringCases?

---

<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:** [April 23, 2013, 8:02pm UTC](https://community.wolfram.com/t/string-matching-with-cases/190/2 "2013-04-23T20:02:34Z")

</div>

This seems to work:

```auto
In[22]:= Cases[ 
   XMLElement[ 
     "myelem", {}, {XMLElement[ 
     "INSDQualifier_value", {}, {"Peptidase family M49; pfam03571"}]}], 
   XMLElement[_, {}, {x_String}] /; StringMatchQ[x, RegularExpression[".+pfam\\d+"]] :> x, Infinity] 
Out[22]= {"Peptidase family M49; pfam03571"} 

```

---

<div class="post-metadata">

**Author:** ![arnoudb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arnoudb/32/41177_2.png) [@arnoudb](https://community.wolfram.com/u/arnoudb)\
**Post date:** [April 23, 2013, 10:00pm UTC](https://community.wolfram.com/t/string-matching-with-cases/190/3 "2013-04-23T22:00:46Z")

</div>

Maybe this:

```auto
Cases[
 XMLElement["myelem", {}, {XMLElement["INSDQualifier_value", {}, {"Peptidase family M49; pfam03571"}]}]
 ,
 x:_String /; StringMatchQ[x, ___ ~~"pfam"~~ DigitCharacter ..] :> StringSplit[x, "; "][[-1]], Infinity]

```

returning this:

```auto
{"pfam03571"}

```
