UareBugged
UareBugged

Reputation: 176

Regex pattern selecting all characters and numbers after specific number until whitespace

I have created a regex pattern that selects a word after a specific word

$string = "Date: Tue, 23 Jun 2015 19:27:30 +0100 Subject: pastik4567"
$pattern = '/(?<=\bSubject:\s)([a-zA-Z-]+)/';
preg_match($pattern,$file,$subjectname);
print_r($subjectname);

result is just pastik not pastik4567 so i`ve tried this

$string = "Date: Tue, 23 Jun 2015 19:27:30 +0100 Subject: pastik4567"
$pattern = '/(?<=\bSubject:\s)([a-zA-Z-]+[0-9]+)/';
preg_match($pattern,$file,$subjectname);
print_r($subjectname);

Now it selects pastik4567 but if subject is, for example, pastik it does not select anything because the pattern wants to match numbers, too.

So I want to ask how to make this pattern "flexible" so that it returns pastik and pastik4567, too.

Upvotes: 0

Views: 54

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627083

Just put the ranges into 1 character class:

$pattern = '/(?<=\bSubject:\s)[a-zA-Z0-9-]+/';

See demo

Note that I placed - at the end of the character class, so that it is treated as a literal hyphen, not a range specifier.

Upvotes: 1

Related Questions