regular expressoin any char including new line

M

MD

regular expression match any character including new line

([.|/s]*)

why this doesn't work?

MD
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

MD said:
regular expression match any character including new line

([.|/s]*)

why this doesn't work?

If think it is common to achieve this functionality
via the Pattern.DOTALL flag.

Arne
 
J

Jeffrey Schwab

MD said:
regular expression match any character including new line

([.|/s]*)

A . inside a character class means the class can match any character
other than newline. You can make it match newlines, too, by making
Pattern.DOTALL the second argument to Pattern.compile.

A | in a character class can match a literal |. The nature of the
character class effectively ORs its sub-expressions, so you do not need
to specify a pipe at all unless you want to match the character |.

The characters / and s inside a character class mean the class can match
either a literal / or literal s. To represent whitespace, use \s.
why this doesn't work?

If nobody has answered your question yet, please post a complete
program, along with sample input.
 
J

Jeffrey Schwab

Jeffrey said:
MD said:
regular expression match any character including new line

([.|/s]*)

A . inside a character class means the class can match any character
other than newline. You can make it match newlines, too, by making
Pattern.DOTALL the second argument to Pattern.compile.

A | in a character class can match a literal |. The nature of the
character class effectively ORs its sub-expressions, so you do not need
to specify a pipe at all unless you want to match the character |.

The characters / and s inside a character class mean the class can match
either a literal / or literal s. To represent whitespace, use \s.
why this doesn't work?

If nobody has answered your question yet, please post a complete
program, along with sample input.

I just realized what you were going for. ITYM ((?:.|\s)*). The better
solution, as Arne suggested, is Pattern.compile(".", Pattern.DOTALL).
 
L

Lasse Reichstein Nielsen

MD said:
regular expression match any character including new line

([.|/s]*)

why this doesn't work?

You are mixing notations. The "|" means either-or outside of a
character class, but it's just the "|" character inside a character
class. (A character class is what is defined by square brackets,
"["..."]"). Also, "/s" should probably be "\s".
Use either
[.\s]
or
.|\s (properly parenthesised: (?:.|\s) and remember that backslash
must be escaped inside string literals),
or better yet, use the DOTALL flag to specify that "." also matches
newlines.

/L
 
J

Jussi Piitulainen

MD said:
regular expression match any character including new line

([.|/s]*)

why this doesn't work?

That pattern matches only sequences that consist of
the four literal characters inside the brackets. The
dot, in particular, "loses its special meaning inside
a character class".
 
L

Lasse Reichstein Nielsen

Lasse Reichstein Nielsen said:

This doesn't work, of course, since "." isn't significant inside
a character class. Instead you can use
[\s\S]
i.e., match any character that is either a whitespace or not a
whitespace.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top