How to replace all strings matching a pattern with correspondinglower case strings ?

Discussion in 'Java' started by anonym, Jan 15, 2009.

  1. anonym

    anonym Guest

    Hi,

    I am trying to process some text content using Java. I want to
    replace all strings matching a pattern with corresponding lower case
    strings. For example, replace all <ABCD> with <abcd>

    I tried the following, but it doesn't work:

    text = text .replaceAll("(<)(.+?)[^(<>)](>)", "$1" + "$2".toLowerCase
    () + "$3");

    Any help is appreciated.
     
    anonym, Jan 15, 2009
    #1
    1. Advertising

  2. anonym wrote:
    > Hi,
    >
    > I am trying to process some text content using Java. I want to
    > replace all strings matching a pattern with corresponding lower case
    > strings. For example, replace all <ABCD> with <abcd>
    >
    > I tried the following, but it doesn't work:
    >
    > text = text .replaceAll("(<)(.+?)[^(<>)](>)", "$1" + "$2".toLowerCase
    > () + "$3");
    >
    > Any help is appreciated.


    import java.util.regex.*;

    public class test {
    public static void main(String[] args) {
    String str = "lak<XYZ>sdfl234k;j<ABCD>;las3kdfl;jk";
    Pattern p = Pattern.compile("(<\\w+>)");
    Matcher m = p.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (m.find())
    m.appendReplacement(sb,m.group(1).toLowerCase());
    m.appendTail(sb);
    System.out.println(sb.toString());
    }
    }

    "$1".toLowerCase() doesn't work. I assume that's because the
    toLowerCase() is applied long before the $1 is replaced by the match.
    This example is pretty much right out of the docs for Matcher.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
     
    Knute Johnson, Jan 15, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max Williams
    Replies:
    0
    Views:
    234
    Max Williams
    Nov 14, 2008
  2. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    260
    Marc Bissonnette
    Jan 13, 2004
  3. DANIEL BURCH

    Pattern Matching on Case

    DANIEL BURCH, Feb 19, 2006, in forum: Perl Misc
    Replies:
    13
    Views:
    200
    DANIEL BURCH
    Feb 20, 2006
  4. Bobby Chamness
    Replies:
    2
    Views:
    264
    Xicheng Jia
    May 3, 2007
  5. penny
    Replies:
    28
    Views:
    3,061
    Charlton Wilbur
    Mar 10, 2008
Loading...

Share This Page