Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
Help with Regex
MattP
For some reason I am really drawing blank on the approach to solve this challenge. Any tips / code samples are appreciated.
I would like to take a dcr value and "truncate" it to the second period. The context is I want to put a meta description tag in the head of a page with the first two lines of the article paragraph.
What would the regex look like to keep everything before the second period and throw away the rest?
So from
This is my dog. This is my cat. This is my bird. This is my iguana.
I want
this is my dog. This is my cat.
Thanks in advance
Matt
Matthew Petitjean
BOC Group
Murray Hill, NJ 07974 USA
Find more posts tagged with
Comments
LooseCannon
one way:
$article = "This is my dog. This is my cat. This is my bird. This is my iguana.";
$article =~ m/(.*?)\.(.*?)\./g;
print "$1.$2.";
MattP
seems like " .* " includes periods, as it skips right over.
I have been trying...
$in =~ s|(\w*)\.(\w*)\.(.*)|$1 $2|;
iwpt_output("$in");
That doesn't work when there are spaces in $in. remove all the spaces. and it workes fine. I have also tried.
$in =~ s|(\w*\s*)\.(\w*\s*)\.(.*)|$1 $2|;
iwpt_output("$in");
I get the whole string, no parsing.
Argh...
Matt
Matthew Petitjean
BOC Group
Murray Hill, NJ 07974 USA
LooseCannon
>>> seems like " .* " includes periods, as it skips right over.
Regexes are **greedy**, they return the longest possible string.
so...
$article =~ m/(.*)\./g;
assigns the value 'This is my dog. This is my cat. This is my bird. This is my iguana' to the variable $1
where
$article =~ m/(.*?)\./g;
assigns the value 'This is my dog' to the variable $1
Hope this helps
Edited by LooseCannon on 04/22/03 11:59 AM (server time).
MattP
Yes, it does, and that works.
Thanks for your help. I appreciate it.
Matt
Matthew Petitjean
BOC Group
Murray Hill, NJ 07974 USA
LooseCannon
glad to help