Looking for a regular pattern

Which pattern represents any characters including space, \t \n?

"Please read before posting

This forum is connected to a mailing list that is read by thousands of
people. Before you post, please use the FAQ, the Ruby documentation and
Google to find an answer to your question. If you can’t find an answer
there, make sure to include all relevant information that is necessary
to help you in your post."

You should only post a doubt if you can’t find the information you need,
not to have someone answer what you can find.

For your question’s answer see this link.
http://www.rubyist.net/~slagell/ruby/regexp.html

Pedro.

Thomas W. wrote:

Which pattern represents any characters including space, \t \n?

It’s \s

I think he actually wants /./m as he said “any character” and not “any
whitespace character”.

HTH,
Sebastian

On Mon, Sep 1, 2008 at 10:20 AM, Zhao Yi [email protected] wrote:

Which pattern represents any characters including space, \t \n?

It’s \s

Sebastian H. wrote:

Thomas W. wrote:

Which pattern represents any characters including space, \t \n?

It’s \s

I think he actually wants /./m as he said “any character” and not “any
whitespace character”.

HTH,
Sebastian

I think this is better [.\s]

Sebastian H. wrote:

Zhao Yi wrote:

I think this is better [.\s]

No, it’s not.

Why? It should match any character.

Zhao Yi wrote:

I think this is better [.\s]

No, it’s not.

Zhao Yi wrote:

Which pattern represents any characters including space, \t \n?

The pattern that matches any character is
/./m
The /m switches the Regexp into multiline mode, and then . matches also
\n.

TPR.

Zhao Yi wrote:

From the docs, http://www.ruby-doc.org/docs/UsersGuide/rg/regexp.html

|\s| space character; same as |[ \t\n\r\f]

That is not any character it is only whitespace.
|

Zhao Yi wrote:

Why? It should match any character.

Yes, but so does just . when you specify the m modifier. So why make it
[.\s]
if you can just do . ?

Hi –

On Mon, 1 Sep 2008, Sebastian H. wrote:

Zhao Yi wrote:

Why? It should match any character.

Yes, but so does just . when you specify the m modifier. So why make it [.\s]
if you can just do . ?

It depends whether you want to use . without including \n somewhere
else in your regex, and therefore don’t want the m modifier.

David

Hi –

On Mon, 1 Sep 2008, Peter H. wrote:

Why? It should match any character.

From the docs, http://www.ruby-doc.org/docs/UsersGuide/rg/regexp.html

|\s| space character; same as |[ \t\n\r\f]

That is not any character it is only whitespace.

Yes, but [.\s] is a character class, consisting of any character other
than \n, plus any space including \n. So it will match any character.

David

David A. Black wrote:

It depends whether you want to use . without including \n somewhere
else in your regex, and therefore don’t want the m modifier.

Good point.

Generally, “.” matches characters including what “\s” matches.

Save your presence, I think you’d better learn more basic regular
expression knowledge first.

2008/9/1 Sebastian H. [email protected]:

Zhao Yi wrote:

Why? It should match any character.

Yes, but so does just . when you specify the m modifier. So why make it [.\s]
if you can just do . ?

One reason would be that you need option /m which is effective for the
whole RX. You can avoid that by doing /(?m:.)/

Cheers

robert

Additionally, you can use pattern “(.|\n)” if you want to match all
characters including “\n” in single line mode.

Hi –

On Mon, 1 Sep 2008, Patrick He wrote:

Generally, “.” matches characters including what “\s” matches.

No, by default . does not match \n. You can make it do so with the /m
modifier.

David

Yes, thank you for reminding me. :slight_smile:

Hi –

On Mon, 1 Sep 2008, Patrick He wrote:

Additionally, you can use pattern “(.|\n)” if you want to match all
characters including “\n” in single line mode.

Ruby regexes don’t really have a single vs. multiple line mode
distinction. They always act as if they have the Perl /m modifier in
place, so that ^ and $ match beginning and end of line but never
beginning and end of string. So Ruby’s default mode is like Perl’s
multi-line mode. Ruby doesn’t really have a single-line mode. /m just
adds \n to the . class, so it’s sort of single line, but ^ and $ still
match lines and not the whole string.

David

On Sep 1, 2008, at 4:27 AM, David A. Black wrote:

From the docs, http://www.ruby-doc.org/docs/UsersGuide/rg/regexp.html

|\s| space character; same as |[ \t\n\r\f]

That is not any character it is only whitespace.

Yes, but [.\s] is a character class, consisting of any character other
than \n, plus any space including \n. So it will match any character.

No, it’s not:

“a” =~ /[.\n]/
=> nil

The period loses its special meaning inside a character class, so that
class literally matches a period or a newline character.

James Edward G. II