Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Daniele Futtorovic Newsgroups: comp.lang.java.programmer Subject: Re: need regular expression to replace part of result based on a search pattern Date: Thu, 12 Jul 2012 00:56:02 +0200 Organization: A noiseless patient Spider Lines: 51 Message-ID: References: <6aefda61-b66a-4e8f-8634-ef6a95f79c4d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Wed, 11 Jul 2012 22:56:49 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="7ecfd50713586de1138e9225bbb530e4"; logging-data="3493"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/b9lVeBQCAADiVvrIThxGD" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20 In-Reply-To: Cancel-Lock: sha1:hVBgLk94PHDTHxBQRVujwi1NmDg= Xref: csiph.com comp.lang.java.programmer:15959 On 12/07/2012 00:40, Knute Johnson allegedly wrote: > On 7/11/2012 10:35 AM, Jimmy wrote: >> Have been having hard time to come up with the regular expression to >> replace the following.. >> >> 1,"blaha",NULL,'N','Y','N','N',NULL >> 2,"blahb",NULL,'Y','Y','Y','N',NULL >> 3,"blahc",NULL,'N','N','N','N',NULL >> ^ >> >> With all contents in column #6 (first column is column #1) to 'Y'? >> >> i.e. result >> >> 1,"blaha",NULL,'N','Y','Y','N',NULL >> 2,"blahb",NULL,'Y','Y','Y','N',NULL >> 3,"blahc",NULL,'N','N','Y','N',NULL >> ^ >> >> Thanks, >> Jimmy >> > > public class test { > public static void main(String[] args) { > String str = "3,\"blaha\",NULL,'N','N','N','N',NULL"; > > System.out.println(str); > > String[] arr = str.split(","); > if (arr[5].equals("'N'")) > arr[5] = "'Y'"; > > StringBuilder sb = new StringBuilder(); > for (int i=0; i sb.append(arr[i]); > if (i != arr.length - 1) > sb.append(","); > } > System.out.println(sb); > } > } > Not handling the case where there is a separator (comma) within a (quoted) field. Regex could only be a solution if such a case were excluded. -- DF.