By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

Regular expression woes

P: n/a
I'm not really sure where to post this question as it covers so many
platforms, but as the platform isn't relevant, here goes...

I'm trying to (pulling my hair out more like) construct a regular
expression string that says the following: "match if the input string
does not start with the characters http". E.g.

e.g.
"this string" - match
"this http string" - match
"http-and-a-bit-more-text" - no match
"ht" - match
"" - match

I've tried something like ^[^(^http)] but this gives no match on the
last 2. Any ideas? - I'd really appreciate it!
Cheers
Mark

Jul 23 '05 #1
Share this Question
Share on Google+
23 Replies


P: n/a
"Mark (News)" <ne**@mail.adsl4less.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I'm not really sure where to post this question as it covers so many
platforms, but as the platform isn't relevant, here goes...


Incorrect. The platform is exceedingly relevant. Regular expressions
are not a constant across languages. Perl regular expression are not
the same as Javascript regular expressions are not the same as PHP
regular expressions.

Choose one or the other, tell us what you're *trying* to do, and in what
environment you're doing it, and then someone can help you.

Paul Lalli

Jul 23 '05 #2

P: n/a
On Fri, 04 Feb 2005 07:19:44 -0800, Mark (News) wrote:
I'm trying to (pulling my hair out more like) construct a regular
expression string that says the following: "match if the input string
does not start with the characters http". E.g.

e.g.
"this string" - match
"this http string" - match
"http-and-a-bit-more-text" - no match
"ht" - match
"" - match


So don't match if the string starts with "http":

$str !~ m/^http/
-leendert bottelberghs
Jul 23 '05 #3

P: n/a

Mark (News) wrote:
I'm not really sure where to post this question as it covers so many
platforms, but as the platform isn't relevant, here goes...

I'm trying to (pulling my hair out more like) construct a regular
expression string that says the following: "match if the input string
does not start with the characters http". E.g.


wouldn't it be:

$match !~ m/^http/;

Is there an equivalent negation metacharacter for a word and not just a
character class? I was just wondering about that.

wana

Jul 23 '05 #4

P: n/a
Mark (News) wrote:
I'm not really sure where to post this question as it covers so many
platforms, but as the platform isn't relevant, here goes...

I'm trying to (pulling my hair out more like) construct a regular
expression string that says the following: "match if the input string
does not start with the characters http". E.g.

e.g.
"this string" - match
"this http string" - match
"http-and-a-bit-more-text" - no match
"ht" - match
"" - match

I've tried something like ^[^(^http)] but this gives no match on the
last 2. Any ideas? - I'd really appreciate it!
Cheers
Mark


Use the "does not match" operator, !~.

if ($my_string !~ /^http/) {
do_something(); }

If you're not using perl, well I guess your platform *is* relevant...
--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
Jul 23 '05 #5

P: n/a
Paul Lalli wrote:
Incorrect. The platform is exceedingly relevant. Regular expressions
are not a constant across languages. Perl regular expression are not
the same as Javascript regular expressions are not the same as PHP
regular expressions.


Also, what you're trying to do - negate a match condition - is often easier
to do in the host language than in the regex itself. For example, in Perl
you could do what you asked with this:

if ($some_string !~ /^http/) { ... }
# or
unless (/^http/) { ... }

But that just reinforces Paul's point - the platform is very relevant.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
Jul 23 '05 #6

P: n/a
I appreciate all the effort in providing a solution to the wider
problem, but perhaps I should have been more explicit - my fault.

I'm specifically trying to avoid using the host shell to do the
negation even though I can use this approach in just about any
language. What I'm really after is to contain the logic entirely within
the regular expression.

Why? Intellectual exercise. :-) (Kind of like why people climb
mountains, but without having to take my butt off the chair.)

Cheers
Mark

Jul 23 '05 #7

P: n/a
Mark (News) wrote on 04 feb 2005 in comp.lang.javascript:
I'm not really sure where to post this question as it covers so many
platforms, but as the platform isn't relevant, here goes...

I'm trying to (pulling my hair out more like) construct a regular
expression string that says the following: "match if the input string
does not start with the characters http". E.g.

e.g.
"this string" - match
"this http string" - match
"http-and-a-bit-more-text" - no match
"ht" - match
"" - match


In javascript this function is not match but test:

var s = "this http string"

if (!/^http/.test(s))
alert("Match!")
else
alert("No match!")

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #8

P: n/a
Mark (News) wrote:
I appreciate all the effort in providing a solution to the wider
problem, but perhaps I should have been more explicit - my fault.

I'm specifically trying to avoid using the host shell to do the
negation even though I can use this approach in just about any
language. What I'm really after is to contain the logic entirely within
the regular expression.


You can do it with a zero-width negative look-ahead assertion in perl.

$string=~/^(?!http)/

--

Rasto Levrinc
http://sourceforge.net/projects/rlocate/
Jul 23 '05 #9

P: n/a
Wow - quite brilliant!

Clearly this was far too easy for you. :-)

Cheers
Mark

Jul 23 '05 #10

P: n/a
Rasto Levrinc wrote:
What I'm really after is to contain the logic entirely within
the regular expression.
You can do it with a zero-width negative look-ahead assertion in perl.

$string=~/^(?!http)/


Some JavaScript implementations implement regular expressions but
don't implement look-ahead assertions. Here you would need

/^([^h]ttp.*|h[^t]tp.*|ht[^t]p|htt[^p].*|.{0,3})$/.test(string)

ciao, dhgm
Jul 23 '05 #11

P: n/a
Dietmar Meier wrote on 04 feb 2005 in comp.lang.javascript:
What I'm really after is to contain the logic entirely within
the regular expression.

You can do it with a zero-width negative look-ahead assertion in perl.

$string=~/^(?!http)/


Some JavaScript implementations implement regular expressions but
don't implement look-ahead assertions. Here you would need

/^([^h]ttp.*|h[^t]tp.*|ht[^t]p|htt[^p].*|.{0,3})$/.test(string)


[The $ cannot be right, I think.]

r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p]))/.test(s)

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #12

P: n/a
Evertjan. wrote:
/^([^h]ttp.*|h[^t]tp.*|ht[^t]p|htt[^p].*|.{0,3})$/.test(string)
[The $ cannot be right, I think.]
For what value of string do you think, the "$" would lead to the
wrong result?
r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p]))/.test(s)


This would not match strings with 3 or less characters.

ciao, dhgm
Jul 23 '05 #13

P: n/a
Dietmar Meier wrote on 04 feb 2005 in comp.lang.javascript:
Evertjan. wrote:
/^([^h]ttp.*|h[^t]tp.*|ht[^t]p|htt[^p].*|.{0,3})$/.test(string)
[The $ cannot be right, I think.]


For what value of string do you think, the "$" would lead to the
wrong result?


"xttp://" should return true
"http://" should return false

Yes, you are right here.
r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p]))/.test(s)


This would not match strings with 3 or less characters.


Yes, you are right again.

Let me try:

r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p])|(.{0,3}$))/.test(s)

[I could loose some () but I like them for clarity

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #14

P: n/a
"Dietmar Meier" <us***************@innoline-systemtechnik.de> wrote in
message news:36*************@individual.net...
Rasto Levrinc wrote:
What I'm really after is to contain the logic entirely within
the regular expression.

You can do it with a zero-width negative look-ahead assertion in
perl.

$string=~/^(?!http)/


Some JavaScript implementations implement regular expressions but
don't implement look-ahead assertions. Here you would need

/^([^h]ttp.*|h[^t]tp.*|ht[^t]p|htt[^p].*|.{0,3})$/.test(string)


Why do people insist on doing things the hardest way possible. Test for
the condition you don't want, then negate it.

if (!/^http/i.test(some_string)) { ... }

By the way, this is pretty much the same solution already provided for
Perl:

if ($some_string !~ /^http/) { ... }

(although I chose to make it case-insensitive, since the protocol in a
URI isn't case-sensitive, it could be upper, lower or mixed case)

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #15

P: n/a
"Why do people insist on doing things the hardest way possible."? Well,
as I said in an earlier post, I wanted to do the whole thing within a
regex rather than resorting to the shell. Mainly because, crazy as it
sounds, it's a fun intellectual exercise. :-) And anyway, if I always
take the path of least resistance, I'll never learn, right? (But I
guess that's OT.)

Jul 23 '05 #16

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in message
news:Xn********************@194.109.133.29...
Dietmar Meier wrote on 04 feb 2005 in comp.lang.javascript:
Evertjan. wrote:
/^([^h]ttp.*|h[^t]tp.*|ht[^t]p|htt[^p].*|.{0,3})$/.test(string)

[The $ cannot be right, I think.]


For what value of string do you think, the "$" would lead to the
wrong result?


"xttp://" should return true
"http://" should return false

Yes, you are right here.
r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p]))/.test(s)


This would not match strings with 3 or less characters.


Yes, you are right again.

Let me try:

r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p])|(.{0,3}$))/.test(s)

[I could loose some () but I like them for clarity


None of those regular expressions will work. For example, you regexp will
not match against "this string", since it differs in 4 places in the first 4
characters.

You cannot negate a string by negating each character. If you really wanted
to do it in that way, you would have to negate all possible combinations of
letters in "http". So, just for fun, it would look something like this
(newlines added for clarity):

/^(
([^h][^t][^t][^p])|

(h[^t][^t][^p])|
([^h]t[^t][^p])|
([^h][^t]t[^p])|
([^h][^t][^t]p)|

(ht[^t][^p])|
(h[^t]t[^p])|
(h[^t][^t]p)|
([^h]tt[^p])|
([^h]t[^t]p)|
([^h][^t]tp)|

(htt[^p])|
(ht[^t]p)|
(h[^t]tp)|
([^h]ttp)

)|(.{0,3}$)/

The moral of this story: "negating" a string in regular expressions is very,
very ugly (without negative look ahead). Your best bet, as many others have
mentioned, is to do something akin to perl's !~, i.e. match against ^http,
and consider matches to be, well, not matches.
Jul 23 '05 #17

P: n/a
Richards Noah (IFR LIT MET) wrote on 04 feb 2005 in
comp.lang.javascript:
r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p])|(.{0,3}$))/.test(s)

[I could loose some () but I like them for clarity


None of those regular expressions will work. For example, you regexp
will not match against "this string", since it differs in 4 places in
the first 4 characters.


s = "this string"
r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p])|(.{0,3}$))/.test(s)
alert(r)

shows: true as per OQ.

So what is the problem?

Please show a string that does not work.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #18

P: n/a
[A complimentary Cc of this posting was sent to
Evertjan.
<ex**************@interxnl.net>], who wrote in article <Xn********************@194.109.133.29>:
r = /^(([^h]...)|(.[^t]..)|(..[^t].)|(...[^p])|(.{0,3}$))/.test(s)


Too much work.

[^h]
| h[^t]
| ht[^t]
| htt[^p]
| .{0,3}$

Hope this helps,
Ilya
Jul 23 '05 #19

P: n/a
Is it true that if zero-width negative look-ahead is not available,
there is always an alternative regex to do the job?

Jul 23 '05 #20

P: n/a
Mark (News) wrote:
"Why do people insist on doing things the hardest way possible."? Well,
as I said in an earlier post, I wanted to do the whole thing within a
regex rather than resorting to the shell.


OK, but that doesn't answer the question. The statement

if (!/^http/i.test(some_string)) { ... }

does not resort to using the shell, and therefore is acceptable,
is it not?

Doing it with a regex + some simple programming is not the same
as resorting to the shell. So, what are your actual requirements?
-Joe
Jul 23 '05 #21

P: n/a
Grant Wagner wrote:
Why do people insist on doing things the hardest way possible.


I don't insist on nothing. Mark announced this as a brainteaser,
nobody is actually expected to use this in a real script.

ciao, dhgm
Jul 23 '05 #22

P: n/a
The if (! some-test) { ... } has the negation as part of the if
statement (I may have erroneously called this negation "using the
shell", whereas it might have been more precise to say "part of the if
statement"). What I was challenging was to achieve the same result, but
keeping any negations inside the RE: if(some-re-test). Hope that makes
sense. My actual requirements are to enjoy solving this challenge -
nothing more. :-)

Jul 23 '05 #23

P: n/a
[A complimentary Cc of this posting was sent to
Mark (News)
<ne**@mail.adsl4less.com>], who wrote in article <11**********************@c13g2000cwb.googlegroups .com>:
The if (! some-test) { ... } has the negation as part of the if
statement (I may have erroneously called this negation "using the
shell", whereas it might have been more precise to say "part of the if
statement"). What I was challenging was to achieve the same result, but
keeping any negations inside the RE: if(some-re-test). Hope that makes
sense. My actual requirements are to enjoy solving this challenge -
nothing more. :-)


Keep in mind that there *is* a legitimate situation when such a
requirement is not bogus: some programs take a REx as a command-line
argument. Given this design, you may be forced to provide some REx
aerobatics if you want squeeze more from such programs.

[If the program is not updated often, sometimes it is easier to change
the source code. ;-]

Hope this helps,
Ilya
Jul 23 '05 #24

This discussion thread is closed

Replies have been disabled for this discussion.