Regular expression tips or resources


Posted on 16th Feb 2014 07:03 pm by admin

Hello! I'm having some issues implementing the appropriate regex patter to eliminate unwanted characters from a string.

Here is a sample string:
Code: String str = "test-hello. me please3, _dog[ -()"; What I need to do is create a regex that will take out all non word characters except for spaces AND strings of type "test-hello" - in other words any string that has word or digit characters followed by a "-" (hyphen) followed by more word or digit characters - this is what is giving me trouble.

What I have right now which is perfect except for the "test-hello" type strings is:
Code: Pattern replace = Pattern.compile("([^\w ])");Matcher matcher = replace.matcher(str);str = matcher.replaceAll(""); The above code removes all non word or digit characters except for spaces but it will obviously still remove the "-" in "test-hello".

I'm not looking for the whole answer of course but any tips or resources I can be pointed to would be greatly appreciated. I'm not familiar with the regex syntax in Java as I am in say PHP.

Thank you!

No comments posted yet

Your Answer:

Login to answer
84 Like 39 Dislike
Previous forums Next forums
Other forums

Lining up columns nicely.
Hello everyone, I'm having problems with making columns line up properly. Here is my code:

Are sessions secure at all?..
I haven't really gotten into yet, but I was just thinking of something weird..

Lets say you h

PHP form authentication
Hi guys,

what am trying to achieve is this: Whenever a user tries to login to my website, an

Php - mysql store data and use it later?
Hi, i' making a login page at the moment, however my username + password is stored many different pl

PHP code needed to get/post form fields and request mysql query results.
I'm new to PHP

I've created an html form to allow the user to request a basic telephone dire

help retrieiving results and doing pagination
Having some trouble trying to get the results to show on more than just one page.

What is ha

MSSQL/PHP
I am tryint to setup a webapplication developed in PHP, Apache2.0 and SQLServer2000 as backend which

Check Digits and extracting digits
I think I can figure out the code for this but I am having a very hard time getting past the first s

empty() error
Why does

Code: empty($USER_ID = $_SESSION["USER_ID"])
create this error...

Count
Getting the same count everytime

Code:
$visitquery = mysql_query("SELECT COUNT(*) FR

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash