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

regular expression question: any character except a white space?

P: n/a
I'm bad at regular expressions. Is this how I would look for any set
of characters that go more than 60 characters without a white space?

..{60}[^ ]

Also, does this match a block of PHP in an HTML page:

\<\?PHP .* \?\>
Jul 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
lawrence wrote:
I'm bad at regular expressions. Is this how I would look for any set
of characters that go more than 60 characters without a white space?

.{60}[^ ]
No, almost ;-)
take this:

[^ ]{60,}
Also, does this match a block of PHP in an HTML page:


If there's no white space between, yes.

Are you looking for wordwrap()?
--> www.php.net/wordwrap

Saludo
Paul.
Jul 17 '05 #2

P: n/a
In article <c1*************@ID-205474.news.uni-berlin.de>,
Paul Wellner Bou <pa**********@united-scripts.com> wrote:
lawrence wrote:
I'm bad at regular expressions. Is this how I would look for any set
of characters that go more than 60 characters without a white space?

.{60}[^ ]


No, almost ;-)
take this:

[^ ]{60,}


That would catch 60 character strings, including those containing
whitespace characters such as newlines, tabs etc. Using preg_match or
preg_match_all:

/\S{61,}/

--
CC
Jul 17 '05 #3

P: n/a
CC Zona wrote:
[^ ]{60,} That would catch 60 character strings, including those containing
whitespace characters...


Excepting the white space character, and that was what he was asking
for. Ok, perhaps he wanted to include other white space characters,
too ;-)
such as newlines,...
Only using modifiers /sm.
tabs etc. Using preg_match or
preg_match_all:

/\S{61,}/


Saludo
Paul.
Jul 17 '05 #4

P: n/a
In article <c1*************@ID-205474.news.uni-berlin.de>,
Paul Wellner Bou <pa**********@united-scripts.com> wrote:
CC Zona wrote:
[^ ]{60,} That would catch 60 character strings, including those containing
whitespace characters...


Excepting the white space character, and that was what he was asking
for.


In article <da**************************@posting.google.com >,
lk******@geocities.com (lawrence) wrote:
I'm bad at regular expressions. Is this how I would look for any set
of characters that go more than 60 characters without a white space?
"White space". Not "space character".
Ok, perhaps he wanted to include other white space characters,
too ;-)

such as newlines,...


Only using modifiers /sm.


Only if the poster chooses PCRE over ereg, which is unlikely for a regex
novice.

$alsowhitespace=array("\n","\r", "\t", "\v");
foreach ($alsowhitespace as $char)
{
$str="01234567890" . $char .
"1234567890123456789012345678901234567890123456789 ";
if (ereg("[^ ]{60,}",$str)) print "Wrong pattern";
}

--
CC
Jul 17 '05 #5

P: n/a
In article <cc**************************@netnews.comcast.net> ,
CC Zona <cc****@nospam.invalid> wrote:
Ok, perhaps he wanted to include other white space characters,
too ;-)

such as newlines,...


Only using modifiers /sm.


Only if the poster chooses PCRE over ereg, which is unlikely for a regex
novice.


(And, only if either of the "s" or "m" modifier applied to the pattern in
question, which neither does. The class [^ ] matches newlines et al in
PCRE as well as ereg.)

--
CC
Jul 17 '05 #6

P: n/a
>>Only using modifiers /sm.

Only if the poster chooses PCRE over ereg, which is unlikely for a regex
novice.


Why that?
(I never worked with ereg... and at php.net they recommend using the
preg functions)
Jul 17 '05 #7

P: n/a
lawrence wrote:
Also, does this match a block of PHP in an HTML page:

\<\?PHP .* \?\>


Did you try it? With no modifiers? All that matches is the string
"<?PHP" followed by a space followed by zero or more of any character
(except newlines, by default), followed by a space, followed by the
string "?>".

The answer to your question, then, is: possibly, depending on the
block's tags, its content and the rest of the file. It matches
almost none of the example blocks given in this newsgroup, for
example.

begin example

<script language=php>
$foo = '<?PHP '; $bar = ' ?>'
</script>

http://www.php.net/manual/en/language.basic-syntax.php

--
Jock
Jul 17 '05 #8

P: n/a
Paul Wellner Bou <pa**********@united-scripts.com> wrote in message news:<c1*************@ID-205474.news.uni-berlin.de>...
lawrence wrote:
I'm bad at regular expressions. Is this how I would look for any set
of characters that go more than 60 characters without a white space?

.{60}[^ ]
No, almost ;-)
take this:

[^ ]{60,}


Thanks much for the response. I've promised myself I'm going to master
regex this winter, but I've still a long way to go.

Are you looking for wordwrap()?
--> www.php.net/wordwrap


As discussed in another thread I posted, I dislike wordwrap on the web
because in most situations I'm combining it with nl2br and it can lead
to jagged line edges. I don't remember why I couldn't put the wordwrap
after the nl2br, but for some reason I couldn't.
Jul 17 '05 #9

P: n/a
ng**********@rediffmail.com (R. Rajesh Jeba Anbiah) wrote in message news:<ab**************************@posting.google. com>...
Try & learn with <http://www.weitz.de/regex-coach/>


Thanks. That was a great suggestion.
Jul 17 '05 #10

P: n/a
I've been using PHP for years now and I still can't do regexs.

Everytime I go to use them, it's like once in a blue moon, so I learn
just enough for that particular requirement, then all the knowledge
leaks out of my head again.

and then next time, it's back to square one.

I bought the oreiley book "mastering regular expressions" but I never
have the time to finish it -- and everytime I come back to it, I have to
start again :/

Everytime I try reading one of the many condensed guides, I go to use it
on my real-life problem and then something is missing. Something is
always missing. It's always a vital detail and I never know what it is
and I never know where to start looking to find the answer.

I don't know how many winters I promised myself I would sit down and
learn it properly. Now that I use XML/XSLT for 100% of my websites, I
have little need for crunching strings.

grrrrrrr

(well that post was completely useless wasn't it).

Jul 17 '05 #11

P: n/a
Terence wrote:
I bought the oreiley book "mastering regular expressions" but I never
have the time to finish it -- and everytime I come back to it, I have to
start again :/
What do you think of what you've read so far?
(well that post was completely useless wasn't it).


I don't think so. But you're welcome to your opinion. :-)

--
Jock
Jul 17 '05 #12

P: n/a
John Dunlop wrote:
Terence wrote:

I bought the oreiley book "mastering regular expressions" but I never
have the time to finish it -- and everytime I come back to it, I have to
start again :/

What do you think of what you've read so far?


I can't remember right now, it's been a few months since I started
reading it. I'll start reading it again and let you know...
Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.