Reputation: 59
Regex is not really my thing. But i have this in my php file and have found it wont work if the string passed has any spaces. Can someone offer an amendment please.
regex:
preg_match('/^(\S+)\/\d/', $_SERVER['HTTP_USER_AGENT'], $matches);
$product_name = $matches[1];
the string will always be in the form of:
1productname/1.0.9 Sparkle/2
everything after the forward slash remains constant before the forward slash can change, and this is the portion i am looking for.
i have found if the first portion contains spaces like:
1 product name/1.0.9 Sparkle/2
this will break.
Upvotes: 2
Views: 93
Reputation: 2709
How about /(.*?)/
if you only want to parse 1productname/1.0.9 Sparkle/2, you begin searching for a forward slash, then you read everyting that comes between the first forward slash and the second one.
Or ^(.*?)/
if you only want to read 1productname/1.0.9 Sparkle/2, space or not space
Upvotes: 0
Reputation: 143091
The regex specifically checks for non-space characters (\S
). Since you asked no question I'll stay content with giving you this explanation.
And it makes sense too. It's pretty reasonable to expect space-separated list of something/numbers signatures in user agent string. And even though they're often also separated by semicolons, it makes no sense to treat space as a part of match.
Upvotes: 1
Reputation: 28316
Try this: /^(.+?)\/\d/
If you weren't trying to match the value after, try this: /^(.+?)\//
Upvotes: 1