• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Tokenize with String.ReplaceAll

 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to tokenize a string. Given a string I want this method to return the output as words. The words have to be words that do not contain superfluous or redundant punctuation that do not add meaning to the word, excluding syle and sacasm. This string may not be well formed, and so, may include edge cases like "awesome!!!", "what??", or "Bye.". To deal with this, i'm trying to see if regular expression will do the job. My problem, here, is that the replaceAll method puzzles me to death. It absolutely does not seem to have an affect on the output. Can you help? Thanks.

 
Marshal
Posts: 26618
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in line 11 of that code you apply replaceAll to the input string (variable name w) and assign the result to a new variable named wnew.

Then at line 12 you apply replaceAll to the input string again, only with a different regex, and assign that result to the variable named wnew. This has the effect of discarding the result of line 11.

Anyway, with regex there's all kinds of unexpected things which can happen if you get it a bit wrong. So it would help if you showed us an example of an input string and the result of line 12. Also it would help if you told us what you expected instead.
 
Marshal
Posts: 72995
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There must be easier ways to do that, even with a regex. Have you come across the predefined character class \w?
 
William Ng
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Well, in line 11 of that code you apply replaceAll to the input string (variable name w) and assign the result to a new variable named wnew.

Then at line 12 you apply replaceAll to the input string again, only with a different regex, and assign that result to the variable named wnew. This has the effect of discarding the result of line 11.

Anyway, with regex there's all kinds of unexpected things which can happen if you get it a bit wrong. So it would help if you showed us an example of an input string and the result of line 12. Also it would help if you told us what you expected instead.



Sorry, I forgot I made those changes. The 'wnew' is supposed to be a w. I did that to so if that would solve the problem. It's the same regardless of whether i create a new variable or not.
An example of I/O would be the following

Input: "This a test...."
Output:
w before replacement: This
w after: This
w before replacement: a
w after: a
w before replacement: test....
w after: test....

The last line should be
w after: test



 
William Ng
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There must be easier ways to do that, even with a regex. Have you come across the predefined character class \w?


I don't doubt that there is. But this is the most simple way that I have come up with, and I have tested its correctess at regexpal.com. But when I use it in replaceAll, it does nothing.
 
William Ng
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have posted the original, before I made the changes in attempt to fix it.
 
Campbell Ritchie
Marshal
Posts: 72995
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Ng wrote:. . . I have tested its correctess at regexpal.com. . . . .

To misquote Edsger Dijkstra, It is impossible to show correctness by testing, only incorrectness.
What you are doing is splitting a String on whitespace, then trying to replace full stops at the start and end of each word by fdsfds or similar? Have you confirmed that your regexes are finding any full stops at all?

You can pass the entire line to a Scanner object, then you can get a Stream of matches from a regex. Look at this method. (Java9 only.) Use its return result's group() method to get a String, and the last line simply calls System.out.println():-
 
William Ng
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
You can pass the entire line to a Scanner object, then you can get a Stream of matches from a regex. Look at this method. (Java9 only.) Use its return result's group() method to get a String, and the last line simply calls System.out.println():-


Thank you for reminding of the quote. I've heard it before, and I there is all truth in that saying.
Yes, I have tried to verify that it finds full stops. And it does not.
I have decided to abandon this method, and chose to write my own finite automaton to handle the splitting.
The goal of the FA is to find words in ill formed text. For example

abab'a, baba baba babbab. "a abab baba baba"     baba-abababa aabab...., ababab???

Should produce the following words

abab'a
baba
baba
babbab
a
abab
baba
baba  
baba-abababa
aabab
ababab

I have already come up with an FA diagram that I will implement soon.

 
Sheriff
Posts: 22211
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Side note: since $ matches the end of the string, $[.]+ will never match anything.
 
Campbell Ritchie
Marshal
Posts: 72995
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:. . . $[.]+ will never match anything.

. . . which is why you aren't matching the ellipses.
 
Campbell Ritchie
Marshal
Posts: 72995
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\\w won't find the apostrophe.
 
Campbell Ritchie
Marshal
Posts: 72995
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:\\w won't find the apostrophe.

Try "[\\w|']+" instead.
 
William Ng
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Side note: since $ matches the end of the string, $[.]+ will never match anything.


I know that. That is why I use [tt]$[\\.]+[tt].
 
William Ng
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll give those suggestions a try, and can you give me your thoughts on using the FA? I think it is the next simplest approach, and probably more efficient also.
 
Campbell Ritchie
Marshal
Posts: 72995
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where I am sitting, FA means something else and you wouldn&apost find it to work. Please avoid such abbreviations.
 
William Ng
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Where I am sitting, FA means something else and you wouldn&apost find it to work. Please avoid such abbreviations.


Hey Cambell Ritchie,
I did mention it unabbreviated, but to save you then need to find it, it stands for 'finite automaton'. I went ahead and created a basic one. You can see it below. And it has been unit tested and works and best as the unit tests says it should.



Here all the tests:

What do you think?
 
reply
    Bookmark Topic Watch Topic
  • New Topic