Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1026
  • Last Modified:

regular expression - retain alpha numeric and spaces

Hi,

I'm a dummy when it comes to regex... I need one that retains characters, numbers and spaces.

It should take performance into consideration.

Thanks
0
MaritimeSource
Asked:
MaritimeSource
  • 2
1 Solution
 
brettmjohnsonCommented:
"[A-Za-z0-9 ]+"
0
 
objectsCommented:
s = s.replaceAll("[^A-Za-z0-9 ]", "");
or
s = s.replaceAll("[^\w ]", "");  // will keep _
0
 
MaritimeSourceAuthor Commented:
Objects,

Are you saying that this:
s = s.replaceAll("[^\w ]", "");

is the same as this:
s = s.replaceAll("[^A-Za-z0-9 ]", "");

except the former retains underscore as well?

I was informed that String.replaceAll is not as fast as using the java.util.regex library. Is this true?
0
 
objectsCommented:
> except the former retains underscore as well?

correct \w matches a-zA-Z_0-9

> I was informed that String.replaceAll is not as fast as using the java.util.regex library. Is this true?

doubt it, replaceAll() uses regex library.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now