regex - replace underscore lowercase with uppercase

前端 未结 4 573
误落风尘
误落风尘 2021-01-17 22:36

I\'ve wondering is there a regex pattern that i could use to convert a pattern which is an underscore and a lowercase letter into an uppercase letter. I\'m trying to generat

相关标签:
4条回答
  • 2021-01-17 23:02

    To do this on regexp level you have to use \U to switch on uppercase mode and \E to switch it off. Here is an example how to use this feature in IntelliJ IDEA find-and-replace dialog which transforms set of class fields to JUnit assertions (at IDE tooltip is a result of find-and-replace transformation):

    0 讨论(0)
  • 2021-01-17 23:13
    import com.google.common.base.CaseFormat;
    
    protected static String replaceDashesWithCamelCasing(String input){
        return CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, input);
    }
    
    0 讨论(0)
  • 2021-01-17 23:18

    Maybe you want to use Google Guava:

    Code:

    import static com.google.common.base.CaseFormat.LOWER_CAMEL;
    import static com.google.common.base.CaseFormat.LOWER_UNDERSCORE;
    
    public class Main {
        public static void main(String[] args) {
            String str = "load_id,policy_id,policy_number";
            for(String columnName : str.split(",")) {
                System.out.println(LOWER_UNDERSCORE.to(LOWER_CAMEL, columnName));
            }
        }
    }
    

    Output:

    loadId
    policyId
    policyNumber
    
    0 讨论(0)
  • 2021-01-17 23:23

    You can use:

    String s = "load_id,policy_id,policy_number";
    Pattern p = Pattern.compile( "_([a-zA-Z])" );
    Matcher m = p.matcher( s );
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase());
    }
    m.appendTail(sb);
    System.out.println(sb.toString()); // loadId,policyId,policyNumber
    
    0 讨论(0)
提交回复
热议问题