• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to change this code with regex  RSS feed

 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a homework assignment. I am trying to find a way to convert this code to do the same thing with regex. I can only use the split, matches, and replaceAll regex methods along with length(), indexOf, charAt. contains, substring, replace and equals. What I want is basically if there is a string "cccaaaadddc" the result to get would be c3a4d3c1. The code below does it, but I need to find a way to add regex into it to do the same function. I can only use a single loop, nested is fine. I can only use these regex symbols:

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

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to come up with a solution but it involved using parentheses. Are you sure those are not permitted?
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately no, I cannot use those.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pity. That would have allowed using groups like this.

Without groups I don't see where using regular expressions buys you anything over the code you already have. Is using regular expressions a requirement?
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah regex is a requirement, also I cannot use matcher or pattern. I tried to code it somewhat and got a Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 4
[a-z]{jj} error. Just trying with nested loops to see what happens before going single.

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you meant
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was debugging the code right now and the problem is that it never goes in the if statement, it always goes in the else statement. I believe I also hit an infinite loop since the program never seems to end.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I consider myself pretty good when it comes to regular expressions and I just don't see where regex's help simplify the code you have. Just to meet the letter of the requirements you could use
and the process the array with a loop similar to your original code.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c is the first character of input
split on everything that's not c
that gives you a string consisting of adjacent c characters
prune off those characters from the beginning of the input string
repeat until input is empty

edit: This is better and shorter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!