• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex for CamelCase  RSS feed

 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to make a camelCase regex for something like "saveChangesInTheEditor" or something like just the String "none". I came across a regex symbol which worked fine with the split method: String [] s2 = s.split("(?<!^)(?=[A-Z])"); I was even trying weird regexPatterns such as [a-z]+[a-zA-Z0-9]+)+" which gave me nothing. The problem with this is that I am restricted to use only these limited regex symbols + I can only use one loop:

.
*
+
\
[]
^
{n} or {n, } or {n-m}

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these restrictions from a homework assignment or from an employer?
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its from a homework assignment, I been trying many combos with split, like for example these take the end of each lower case, but the start of each upper case which is close, but not quite what i need: [a-z]{1}[A-Z]+
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me see if I understand what you're trying to do: Do you want to split up a string like "someCamelCaseWords" into separate words like {"some", "Camel", "Case", "Words"}?
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes so I am trying to make a pattern that makes "saveChangesInTheEditor" into "save Changes In The Editor" or the string "none" to "none". So I thought about doing the split method and got various results.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think split() will work for what you're trying to do because it throws away any characters that are deemed to be "separators".  You don't want to throw away any characters, you want to keep all of them. I would say that if you really want to use regular expressions to do this, you need one that allows you to extract groups of characters from the input and you define a group as starting with a capital letter. You have to account for the first word, too, if it isn't capitalized.
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright I got it, since I am new to regex I still do not have a solid grip on its methods. I was trying to use split method which was wrong as suggested and used matches method instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!