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
Solved

java string split with regular expression

Posted on 2015-02-06
8
108 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 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