Solved

java string split with regular expression

Posted on 2015-02-06
8
96 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 80
countHi2 challenge 7 44
Eclipse Neon and jdk 1.8.0 11 66
maven example error 3 35
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

708 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

17 Experts available now in Live!

Get 1:1 Help Now