Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

java string split with regular expression

Posted on 2015-02-06
8
Medium Priority
?
122 Views
Last Modified: 2015-02-06
Hi Experts,

Can someone give me a regular expression that would allow me to:

turn this:  ".exe; .bat, .ui.xml."

into this:    "exe bat ui.xml"

In other words, I want to remove leading and trailing '.' characters and the delimiters are commas, spaces and semi-colons....

Thanks a lot!
Mike
0
Comment
Question by:thready
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40593843
String resultString = subjectString.replaceAll("[.;,] [.;,]|^[.;,]|[.;,]$", " ");

Open in new window

Then simply delete the spaces at the beginning and end.

HTH,
Dan
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40593857
into this:    "exe bat ui.xml"
That doesn't look right. The last file extension type is joined to something else
0
 
LVL 1

Author Comment

by:thready
ID: 40594031
That's intentional.  There are files that have double extensions - like .ui.xml files.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:thready
ID: 40594048
Thanks Dan!  That's really close - but it didn't work for this one: "..exe;., .bat, .ui.xml" - which I think is still a valid extension list...  I need to be picky here, sorry.  I'm trying to understand your regex...
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 2000 total points
ID: 40594205
String resultString = subjectString.replaceAll("(?m)^[.;,]+|[.;,]+ [.;,]+|[.;,]+$", " ");

Open in new window

You did not specify on the OP that there can be multiple consecutive .,;

The pattern is quite simple:
1. search for some .;, at the beginning of the string
or (| means OR)
2. search for some ,;. plus space plus some ,;. inside the string
or
3. search for some .;, at the end of the string.

If found, replace with a single space.
0
 
LVL 1

Author Closing Comment

by:thready
ID: 40594398
Very awesome.  Thanks a lot!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40594403
You're welcome.
Glad I could help!
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40594416
Update: if you use this
String resultString = subjectString.replaceAll("(?m)^[.;,]+|[.;,]* [.;,]*|[.;,]+$", " ");

Open in new window

it will also work for
.bat. ui.xml..
.bat .ui.xml..
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question