tags:

views:

96

answers:

4

I need to match any kind of string which has at least a . (dot) and a * (star) character and not a < or > (html tags). Any ideas? Thanks.

A: 
^[^<>]*(\.[^<>]*\*|\*[^<>]*\.)[^<>]*$

For further experimentation, use rubular, a live regex checker.

The MYYN
Fails. Has to have a dot AND a star.
Zano
That's dot *or* star. I think he wants dot *and* star.
Mark Byers
+4  A: 

I think the following would work, but I don't think regex is the easiest way to solve this problem.

^[^<>]*(\.[^<>]*\*|\*[^<>]*\.)[^<>]*$
Mark Byers
as you said: http://stackoverflow.com/questions/58640/great-programming-quotes/58646#58646 ;-)
Kai
A: 

^ [^<>]* ( \. [^<>]* \* | \* [^<>]* \.) [^<>]* $ (sans the spaces), but two regexes would be easier.

In Perl: /^[^<>]*\.[^<>]*$/ and /^[^<>]*\*[^<>]*$/

Zano
+2  A: 

^([^<>.]*\.[^<>*]\*)|([^<>*]*\*[^<>.]\.)[^<>]*$

This is similar to the answer provided by Mark Byers, but should be more efficient because it reduces backtracking.

Explanation: The initial ^ character specifies that the pattern applies to the entire string. Then, the string can contain one of two patterns:

  • A period followed by zero or more characters that are not < or >, and an asterisk, OR
  • An asterisk followed by zero or more characters that are not < or >, and a period.

After either of those is satisfied, the string can contain any number (zero or more) characters that are not < or >.

Jim Mischel
Voted up for providing an explanation.
Shoko
Doesn't solve the problem though. dot AND asterisk. Both conditions must be met.
Zano
Almost.. the parenthesis could be moved as follows.. ^([^<>.]*\.[^<>*]*|[^<>*]*\\*[^<>.]\.)[^<>]*$ but is a potentially more efficient query.
PP