I need to create a regular expression to validate a Camelcase

Asked

Viewed 279 times

1

Good morning to everyone, I hope you can help me. I need a regex or even an idea of how to insert a space for Camelcase expressions. In other words, insert a space for each uppercase letter of the word. Java...

1 answer

0


Regex : /[A-Z]/g

This way you can find all the uppercase.

Testing

So in code you can use something like:

String s = "TudoMaiúsculoSemEspaçoQuemFezIsso";
StringBuilder out = new StringBuilder(s);
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(s);
int extraFeed = 0;
while(m.find()){
    if(m.start()!=0){
        out = out.insert(m.start()+extraFeed, " ");
        extraFeed++;
    }
}
System.out.println(out);
  • Why did you use Matcher and Pattern?

  • And Thanks worked here for me, I just wanted an explanation for having used the two classes q I quoted Matcher and Pattern

  • @Stan https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.