We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

quick regex exercise with preg_match()

jeremyBass26
jeremyBass26 asked
on
Medium Priority
449 Views
Last Modified: 2012-05-06
Hello, I have a quick regex exercise I need to get be fore I can move on and for the life of me it has been stumping me for many hours to day... could some one shed a little light on this please thank you..

jeremyBass


from
         $category_name ="HARD DRIVES - WESTERN DIGITAL IDE";
         preg_match('/[^?*](\s\-\s)[^?*]/', $category_name, $matches);
                  echo "Main Cat is: {$matches[0]}\n";
            echo "Sub Cat is: {$matches[2]}\n";


so I'd have

Main Cat is: HARD DRIVES
Sub Cat is: WESTERN DIGITAL IDE


but what I get is
Main Cat is: S - W
Sub Cat is:
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
did you mean
'/(.*)\s\-\s(.*)/'

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:

$category_name ="HARD DRIVES - WESTERN DIGITAL IDE";
preg_match('/^([a-zA-Z ]*?)(\s\-\s)(.*)/', $category_name, $matches);
echo "Main Cat is: {$matches[1]}<br>\n";
echo "Sub Cat is: {$matches[3]}<br>\n";

Open in new window

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Output from that is:
Main Cat is: HARD DRIVES
Sub Cat is: WESTERN DIGITAL IDE

Open in new window

Author

Commented:
preg_match('/^([a-zA-Z ]*?)(\s\-\s)(.*)/', $category_name, $matches);

works... but what if it was

kdi4ds s dkf52!  -  kdirsl kd slF9

is it general enough for that?

thank for the help...

Author

Commented:
it was alittle bit of both...
preg_match('/^(.*)(\s\-\s)(.*)/', $category_name, $matches);

thanks for the help


cheers
jeremy
CERTIFIED EXPERT

Commented:
The pattern in the accepted solution would be generic enough to match all such strings containing a '-' character.
<pre>
<?php
$category_name ="kdi4ds s dkf52!  -  kdirsl kd slF9";
echo preg_match('/(.*)\s\-\s(.*)/', $category_name, $matches) ? "Main Cat is: {$matches[1]}\nSub Cat is: {$matches[2]}\n" : "no match found." ;
?>

Open in new window

Author

Commented:
hey there ddrudik... rock on.. that works great... probably what was meant in the first one now that I look at it...

think you could look at this... and double check the suggestions to see if they are suppost to work?

http://www.experts-exchange.com/Programming/Languages/Regular_Expressions/Q_24121684.html

cheers
jeremy
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.