2 Replies - 2657 Views - Last Post: 14 March 2009 - 01:32 AM Rate Topic: -----

#1 squarewavedreams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-March 09

Matcher.replaceAll()... What am I doing wrong?

Posted 13 March 2009 - 07:23 PM

Ok so... This is part of a 'translator' that takes files in a certain regular language and writes them out in a new regular language. Part of the stipulation is that all the important bits HAVE to use java.util.regex, otherwise I'd already be done with this. No problems until I got to the actual translating part.

Long story short, I have a string of Morse Code, for example ".-- -.. ---" (By the professor's key, which I think is wrong morse code D: anyway it's supposed to say 'Dad'). My best instinct is to go through all the various Morse Code symbols and use Matcher.replaceAll() to replace first all the A's, then all the a's, then all the B's, etc. etc... So here's what I came up with:
for (String symbol : keyList) {
	String newSymbol = valueList.get(keyList.indexOf(symbol)); // The lists have, of course, the source and target symbols
	sourceText = Pattern.compile("\b" + symbol + "\b").matcher(sourceText).replaceAll(newSymbol);
}



Problem: It doesn't work. The program completes and there are no changes from the source file to the target file. No translation, just the same old morse code string.

Here's what else I've tried:

I tried omitting the \b's. That caused the matcher to ignore the whitespace and just attack every single A it saw right off the bat. So with the above string I got something ridiculous like AB- or something.
I also tried using \\b instead of \b, and supplying 'Pattern.LITERAL' as the second argument. Again, no change. Same for if I only omit one \b or the other.

Suggestions? =/

This post has been edited by squarewavedreams: 13 March 2009 - 07:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Matcher.replaceAll()... What am I doing wrong?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2777
  • View blog
  • Posts: 11,757
  • Joined: 20-September 08

Re: Matcher.replaceAll()... What am I doing wrong?

Posted 14 March 2009 - 01:24 AM

I think you'll find _ . . |. - | _ . . is Dad ;-)

Anyway, one of the things you tried should have worked, although it would be easier to do


s = s.replaceAll(String.format("\\b%s\\b", newSymbol));


This post has been edited by g00se: 14 March 2009 - 01:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Matcher.replaceAll()... What am I doing wrong?

Posted 14 March 2009 - 01:32 AM

Have a look at Class Hashtable Use the Morse code character as the key, and the ASCII character as the value. Then, you can simply scan the input and convert the code to characters on the fly.

Opps, I didn't see the "HAVE to use java.util.regex" part. NVM. :)

This post has been edited by n8wxs: 14 March 2009 - 01:34 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1