Question: I'm stuck writing a preg\_match

I have a string:

```
XPMG_ar121023.txt

```

and need to extract the 2 letters between `XPMG_` and the first digit - be it a 0-9

```
$str = 'XPMG_ar121023.txt';
preg_match('/('XPMG_')|[0-9\,]))/', $str, $match);
print_r($match);

```

Maybe this isn't the best option: My characters will always be

Answer: 
Let me walk through this step by step so as to help you solve similar problems in the future. Suppose we have the following format for our filenames:

```
XPMG_ar121023.txt

```

We know what we want to capture, we want the "ar" right after the \_ and just before the numbers begin. So our expression would look something like this:

```
_[a-z]+

```

This is pretty straight-forward. We're starting by looking for an underscore, followed by any number of letters between a and z. The square brackets define a character class. Our class consists of the alphabet, but you can push specific numbers in there and more if you like.

Now because we want to capture only the letters, we need to put parenthesis around that part of the pattern:

```
_([a-z]+)

```

In the result we will now have access to only that subpattern. Next we put our delimiters in place to specify where our pattern begins, and ends:

```
/_([a-z]+)/

```

And lastly, after our closing delimiter we can add some modifiers. As it is written, our pattern only looks for lower-case letters. We can add the `i` modifier to make this case-insensitive:

```
/_([a-z]+)/i

```

Voila, we're done. Now we can pass it into `preg_match` to see what it spits out:

```
preg_match( "/_([a-z]+)/i", "XPMG_ar121023.txt", $match );

```

This function takes a pattern as the first parameter, a string to match it against as the second, and lastly a variable to spit the results into. When all is said and done, we can check `$match` for our data.

The results of this operation follow:

```
array(2) { 
    [0]=> string(3) "_ar" 
    [1]=> string(2) "ar" 
}

```

This is the contents of `$match`. Notice our full pattern is found in the first index of the array, and our captured portion is provided in the second index of the array.

```
echo $match[1]; // ar

```

Hope this helps.
Well, why not:

```
$letters = $str[5].$str[6];

```

:)
After all, you'll always need the 2 chars after the fixed prefix, there are many ways that do not require a regexp (`substr()` being the best anyway)