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
2009-11-23 23:36:41
Fails. Has to have a dot AND a star.
Zano
2009-11-23 23:37:40
That's dot *or* star. I think he wants dot *and* star.
Mark Byers
2009-11-23 23:38:10
+4
A:
I think the following would work, but I don't think regex is the easiest way to solve this problem.
^[^<>]*(\.[^<>]*\*|\*[^<>]*\.)[^<>]*$
Mark Byers
2009-11-23 23:39:29
as you said: http://stackoverflow.com/questions/58640/great-programming-quotes/58646#58646 ;-)
Kai
2009-11-23 23:49:50
A:
^ [^<>]* ( \. [^<>]* \* | \* [^<>]* \.) [^<>]* $
(sans the spaces), but two regexes would be easier.
In Perl: /^[^<>]*\.[^<>]*$/ and /^[^<>]*\*[^<>]*$/
Zano
2009-11-23 23:41:15
+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
2009-11-23 23:51:54