Reply
Regex Problem
Old 03-07-2007, 07:46 AM Regex Problem
Junior Talker

Posts: 1
Name: Brad Stemp
Hi

I'm trying to come up with a regex match that will match a string that does NOT contain an underscore "_". i.e match "foo" but not "foo_bar"

Second part of my question is for words that do contain underscores - I want to retrieve everything before the underscore. i.e. for "foo_bar" match and return "foo"

Thanks
bstemp is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-07-2007, 11:03 AM Re: Regex Problem
Insensus's Avatar
Ultra Talker

Posts: 275
Name: Mark Stegeman
Location: Netherlands, Europe
1:
PHP Code:
<?php
$str 
"test_";
if(!
preg_match("/_/",$str)){
echo 
"No underscore.";
}
?>

2:
PHP Code:
<?php
$string 
"test_ing";
$arr explode("_",$string); // Split the string on "_"
echo $arr[0]; // Echo the part before the "_"
?>
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 03-07-2007, 11:25 AM Re: Regex Problem
Insensus's Avatar
Ultra Talker

Posts: 275
Name: Mark Stegeman
Location: Netherlands, Europe
1:
If you really want to do it with only a regex and not with the '!':
PHP Code:
<?php
$str 
"test";
preg_match("/[^_]*/",$str,$matches);
if(
strlen($matches[0])<strlen($str)){
echo 
"Underscore spotted!";
}
?>
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Regex Problem
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.11054 seconds with 12 queries