473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Regular expression woes

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
23 1799
"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
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

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
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
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
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
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
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
Wow - quite brilliant!

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

Cheers
Mark

Jul 23 '05 #10
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
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
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
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
"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
"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
"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
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
[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
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
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
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
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
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rob Pridham | last post by:
Hi. I'm trying to use ereg_replace to turn some text based hyperlinks into WML code. WML is similar to HTML, except for mobile phones and very strict. That means that there's some characters you...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
0
by: Jim Mace | last post by:
I have been racking my brain trying to figure out a regular expression that will replace all of the characters in a string except the very first character from uppercase to lower case. Any ideas. I...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
9
by: Mark Rae | last post by:
Hi, This time, I'm looking for a regular expression which says "the string must contain exactly seven or exactly eight digits" e.g. 123456 fails 1234567 passes 12345678 passes 123456789...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.