• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to change this code with regex

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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}

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately no, I cannot use those.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you meant
 
Abad Ashraf
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic