Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex to match say char 't' exactly once in a string and no more than once

Reply
Thread Tools

Regex to match say char 't' exactly once in a string and no more than once

 
 
Gancy
Guest
Posts: n/a
 
      02-03-2005
Some times Regex's which are supposed to be simple eats head. I have
come across one such, though looks very simple, i am frustrted to right
the expressions for it.

I want to match a specifice charecter in a string, say 't', exactly
once anywhere in the string, but should not match 't' more than once.
sounds simple is it not.

 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      02-03-2005
Gancy wrote:
> I want to match a specifice charecter in a string, say 't', exactly
> once anywhere in the string, but should not match 't' more than once.
> sounds simple is it not.


What have you tried so far? Have you checked the Perl documentation,
especially the FAQ? Hint:

perldoc -q count


--
Tore Aursand <(E-Mail Removed)>
"If you can't annoy somebody with what you write, I think there's
little point in writing." (Kingsley Amis)
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-03-2005
Tore Aursand <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Gancy wrote:
> > I want to match a specifice charecter in a string, say 't', exactly
> > once anywhere in the string, but should not match 't' more than once.
> > sounds simple is it not.

>
> What have you tried so far? Have you checked the Perl documentation,
> especially the FAQ? Hint:
>
> perldoc -q count


That discusses counting of substrings, but not matching if there is
only one occurrence.

Now, in general it *is* the better solution to count and see if the
result is 1, especially if going for multi-character strings or even
general subpatterns.

For the problem of matching single occurrences of a *single character*,
there is a simple regex solution:

my $char = 't';
my $re = /
^ # beginning of string
[^$char]* # any number of characters different from $char
$char # a single $char
[^$char]* # any number of characters different from $char
$ # end of string
/x;

Anno
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-03-2005
Gancy <(E-Mail Removed)> wrote:

> I want to match a specifice charecter in a string, say 't', exactly
> once anywhere in the string, but should not match 't' more than once.



/^[^t]*t[^t]*$/


> sounds simple is it not.



Looks pretty simple to me...


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Rasto Levrinc
Guest
Posts: n/a
 
      02-03-2005
Gancy wrote:

> I want to match a specifice charecter in a string, say 't', exactly
> once anywhere in the string, but should not match 't' more than once.
> sounds simple is it not.
>


Here is funny way to do it without using regexp.

if (abs index($string, $char) == rindex($string, $char)) {
...
}

--

Rasto Levrinc
http://sourceforge.net/projects/rlocate/
 
Reply With Quote
 
 
 
Reply

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
error: no match for ‘operator<<’ in ‘std::cout.std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](k) << dispMyarr(k)’ curiousEngine C++ 1 05-09-2008 09:34 AM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
Regex exp again... need to match exactly two letters. Merlin Perl Misc 3 01-31-2004 11:09 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM



Advertisments