Solved

java string split with regular expression

Posted on 2015-02-06
8
99 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
  • 4
  • 3
8 Comments
 
LVL 34

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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 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 34

Expert Comment

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 100
advertisement module in core php 4 145
recursion example 16 111
thymeleaf natural templating vs JSP 2 67
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now