?
Solved

java string split with regular expression

Posted on 2015-02-06
8
Medium Priority
?
116 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
Technology Partners: 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!

 
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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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
Course of the Month10 days, 23 hours left to enroll

770 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