Results 1 to 4 of 4
  1. #1
    sozeee is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default regular expressions

    I'm writing a program that modifies a string by capitalizing substrings between <upcase> </upcase> tags and removes tags, using regular expressions. How does capitalising itself work? I'm so far here

    Java Code:
    public class string3 {
    
    	public static void main(String[] args) {
    		String str = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";
    		System.out.println(str);
    		String replacedStr = str.replaceAll("(<upcase>)(.*?)(</upcase>)", "$2");
    		System.out.println(replacedStr);
    		
    
    	}
    
    }
    ...but I can't get the substrings in caps.

  2. #2
    f1gh is offline Member
    Join Date
    Nov 2010
    Posts
    46
    Rep Power
    0

    Default

    something like:
    Java Code:
    //keep in mind this array is only of size 2 so if you have more words between uppercase you need to use bigger array
    String brokenup[] = new String[2];
    
    //break the two words between uppercase into individual words
    brokenup = replacedStr.split(" ");
    
    //get the first letter of the first word and capitalize it
    String firstWordFirstLetter = brokenup[0].chartAt(0).toUpperCase();
    
    //create the first word by combining the capitalized first letter with rest of the word
    String firstWord = firstWordFirstLetter + borkenup[0].substring(1,brokenup[0].length -1);
    
    //basically this does same as what we did for above word
    String secWordFirstLetter = brokenup[1].charAt(0).toUpperCase();
    String secWord = secWordFirstLetter + brokenup[1].substring(1,brokenup[1].length-1);
    System.out.println(firstWord+" "+secWord);
    this is a rough idea of how it should work. Didn't test this so might have to correct errors but this gives you an idea. This is not the most simplest way nor shortest code to do this i am sure it is possible to do this without creating so many vars and statements.

  3. #3
    sozeee is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks! I was thinking if there was some regex operator for the replacing expression, something like "\\U" which I found somewhere on the net but it doesn't seem to work. Or maybe someway to use the toUpperCase() method on the replacing part, so that the whole thing goes with a few lines.

  4. #4
    sozeee is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Using regular expressions for search
    By carderne in forum New To Java
    Replies: 9
    Last Post: 05-24-2009, 05:58 AM
  2. Regular Expressions in java
    By blue404 in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2008, 04:43 AM
  3. Using Quantifiers in regular expressions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-10-2008, 11:43 AM
  4. Handling regular expressions using Regex
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 01-07-2008, 01:46 PM
  5. Regular expressions quantifiers
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-25-2007, 12:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •