Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Jim Janney Newsgroups: comp.lang.java.programmer Subject: Re: Regex doesn't recognize single quote Date: Sat, 07 Jan 2012 19:02:04 -0700 Organization: reply hazy, try again Lines: 34 Message-ID: <2pr4zbq7dv.fsf@shell.xmission.com> References: <74f4b448-24bf-448f-9f4a-06fd1b79c86d@o12g2000vbd.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="PnllQd880uOddfy6hsxHuQ"; logging-data="5930"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+BquYhXS0iqqSWUrQQeguK" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:kZmeWCTPZpi6vnG9cx3kHifozx8= sha1:TVn9sj9l+kkuJthJ5i0d/5vwvco= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11101 Daniel Pitts writes: > On 1/6/12 2:08 PM, Jerric wrote: >> Hi, I need to remove special characters, except \w and single quotes, >> from a string, can someone please help me on the regex? >> >> for example, I have "ab'de+fg", I want to get "ab'defg", and I tried >> the following code, but it removed single quote. seems to me java >> cannot handle the pattern like [^']. >> >> String val = "ab'de+fg"; >> val = val.replaceAll("[^\\w']+", ""); >> >> Thanks a lot, > It works for me, which indicates the problem is somewhere in the code > you didn't post. Here is an SSCCE: > > public class Works { > public static void main(String[] args) { > String val = "ab'de+fg"; > System.out.println(val.replaceAll("[^\\w']+", "")); > > } > } > > Try posting exactly the code which causes the problem. Since replaceAll is being used, the closure is unnecessary, so this can be shortened by one character :-) -- Jim Janney