Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 2nd, 2008, 04:03 PM
Authorized User
Join Date: Oct 2006
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default New help with Pattern for special characters


 I am currently working with Pattern and Matcher in Java.
 I have the following strings:

    Boys play balls. Girls play dolls. Baby likes bottles.

 I like to create a pattern to find the period and space(s) in the string. This is my pattern so far, ".\\s+". It doesn't work because the "." in my pattern could mean any character.

How do I make my pattern to recognize it as a period, and not any character.

Please help.

Old July 2nd, 2008, 04:17 PM
Friend of Wrox
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts


Most special characters can be escaped by preceding them with a back slash. (Which of course needs to be two slashes because Java treats the single one as its *own* escape.)

So, the pattern would be

I tend to escape all special characters at all times, just for the paranoia of it. Even when I don't have to.

For example, I'll tend to use
even though I know in my head that no backslashes are needed there. But guess what: Most of the time my patterns work first time. I'll take the extra typing in place of the extra debugging.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Special Characters not working for me Russ XSLT 1 June 18th, 2008 05:21 PM
querystring and special characters tulincim Classic ASP Basics 4 October 4th, 2005 06:05 PM
storing special special characters in nvarchar... ACE2084 SQL Server 2000 2 February 9th, 2005 11:45 AM
special characters lian_a Classic ASP Basics 3 June 23rd, 2004 05:16 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.