Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Parsing a string in Java with multiple delimiters
New Posts  All Forums:Forum Nav:

Parsing a string in Java with multiple delimiters

post #1 of 3
Thread Starter 
I'm trying to load a string into an array, removing all whitespace and punctuation. The problem is, if I have two delimiters in a row, it leaves a blank in the array.

What I have:
Code:
String[] temparray =inputstr.split("\\s+|,|;|\\(|\\)|, ");

Example:
inputstr = "I bought bread, eggs, and milk"

It outputs as:
I
bought
bread

eggs

and
milk

Anyone know how to keep the spaces after the commas from being loaded into the array?
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
post #2 of 3
Code:
String[] temparray =inputstr.replaceAll("[^a-zA-Z ]", "").split("\\s+");

Remove punctuation, then split on whitespace
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
Core I7
(13 items)
 
  
CPUMotherboardGraphicsRAM
I7 920 rev. D0 @ 4.26Ghz EVGA X58 SLI EVGA GTX 285 OCZ XMP 3x2Gb (pc3 12800) 
Hard DriveOptical DriveOSMonitor
Western Digital Caviar Black 640Gb x 2 LG GH22LS30 openSuse 12.1 x64 HP F2105 
PowerCase
CORSAIR 850TX Cooler Master ATCS 840 
  hide details  
Reply
post #3 of 3
Thread Starter 
Quote:
Originally Posted by tom.slick View Post

Code:
String[] temparray =inputstr.replaceAll("[^a-zA-Z ]", "").split("\\s+");

Remove punctuation, then split on whitespace

Thanks, this worked.
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
Desktop
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 6600k ASUS Z170-PRO ZOTAC GeForce GTX 970 4GB  Kingston HyperX Fury 32GB (2 x 16G) DDR4 2400 
Hard DriveOptical DriveCoolingOS
Samsung 840 Pro 128GB + Seagate 1TB SATA LG Blu-Ray Corsair H60 Windows 10 Home 64-bit 
MonitorKeyboardPowerCase
24" ASUS VG248QE CM Storm QuickFire TK Corsair CMPSU-850TX 850W Phanteks Enthoo Pro 
MouseAudio
Corsair Vengeance M65 ASUS Xonar U7 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Parsing a string in Java with multiple delimiters