Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11100
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail |
|---|---|
| From | Lew <noone@lewscanon.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Regex doesn't recognize single quote |
| Date | Sat, 07 Jan 2012 17:20:41 -0800 |
| Organization | albasani.net |
| Lines | 42 |
| Message-ID | <jear17$320$1@news.albasani.net> (permalink) |
| References | <74f4b448-24bf-448f-9f4a-06fd1b79c86d@o12g2000vbd.googlegroups.com> <prmfg79jlt8o86otpnabqfs924cc399var@4ax.com> <bloat-20120107123558@ram.dialup.fu-berlin.de> <tphhg71jbmq4q2vj5dtno5r01igvgdavh2@4ax.com> <jeaj4f$2dm$1@speranza.aioe.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.albasani.net h/KcB4js7t0skvGEAEW9pKuFsptJPHFRGvI6Q6SSwF9lc2XvgujxWshk5d3vttmVMkakLVVo23R6hdxNVvjKCc7MzuAJfgQcjoYak+OFG9AXXERgeQ9ScyoLGEe1qiXh |
| NNTP-Posting-Date | Sun, 8 Jan 2012 01:20:39 +0000 (UTC) |
| Injection-Info | news.albasani.net; logging-data="GiaZs6vVGPBZbfWw31e9VWgVvMPV90Z4RoKQaFbyBrrA7Vhk/w8or+GzBaeiRTUuE21f2azsdFLg3uaY8W2aVCmnnkUpkM4eQP1Blu0zN0SOQxzHl+mZqOsEX0ZxOIbI"; mail-complaints-to="abuse@albasani.net" |
| User-Agent | Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16 |
| In-Reply-To | <jeaj4f$2dm$1@speranza.aioe.org> |
| Cancel-Lock | sha1:FOcOaTdvggR5wjRFUX/Q/yba7Is= |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:11100 |
Show key headers only | View raw
> Roedy Green wrote:
>> What is your simpler implementation?
>>
>> /** remove ' and \w from string
>> * @param s string to process
>> * @return string without ' or \w
>> */
>> private static String scrunch( final String s )
>> {
>> final Stringbuilder sb = new StringBuilder( s.length() );
>> for (int i=0; i<s.length(); i++ )
>> {
>> char c = s.charAt(i);
>> if ( !( c = '\'' || c = '\w' ) )
>> {
>> sb.append ( c );
>> }
>> }
>> return sb.toString();
>> }
That will not perform the specified action, which is to remove non-word
characters and to _keep_ apostrophes. '\w' is not legitimate Java syntax,
thus will cause a compilation error.
"It is a compile-time error if the character following a backslash in an
escape is not an ASCII b, t, n, f, r, ", ', \, 0, 1, 2, 3, 4, 5, 6, or 7."
<http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.6>
The simpler approach was already posted by Daniel Pitts, and has the added
virtues of both meeting the requirement and compiling:
public class Works {
public static void main(String[] args) {
String val = "ab'de+fg";
System.out.println(val.replaceAll("[^\\w']+", ""));
}
}
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Regex doesn't recognize single quote Jerric <jerricgao@gmail.com> - 2012-01-06 14:08 -0800
Re: Regex doesn't recognize single quote Martin Gregorie <martin@address-in-sig.invalid> - 2012-01-06 22:23 +0000
Re: Regex doesn't recognize single quote Jake Jarvis <pig_in_shoes@yahoo.com> - 2012-01-06 23:48 +0100
Re: Regex doesn't recognize single quote Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-06 15:49 -0800
Re: Regex doesn't recognize single quote Jim Janney <jjanney@shell.xmission.com> - 2012-01-07 19:02 -0700
Re: Regex doesn't recognize single quote Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-09 09:58 -0800
Re: Regex doesn't recognize single quote Roedy Green <see_website@mindprod.com.invalid> - 2012-01-06 21:47 -0800
Re: Regex doesn't recognize single quote Roedy Green <see_website@mindprod.com.invalid> - 2012-01-07 14:41 -0800
Re: Regex doesn't recognize single quote Rafael Villar <morgano5@hotmail.com> - 2012-01-08 09:05 +1000
Re: Regex doesn't recognize single quote Rafael Villar <morgano5@hotmail.com> - 2012-01-08 09:10 +1000
Re: Regex doesn't recognize single quote Lew <noone@lewscanon.com> - 2012-01-07 17:20 -0800
Re: Regex doesn't recognize single quote Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-09 10:08 -0800
Re: Regex doesn't recognize single quote Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-01-09 10:33 -0800
Re: Regex doesn't recognize single quote Roedy Green <see_website@mindprod.com.invalid> - 2012-01-07 14:48 -0800
Re: Regex doesn't recognize single quote Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2012-01-07 20:58 -0400
csiph-web