aspose file tools*
The moose likes Java in General and the fly likes Regular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular expression" Watch "Regular expression" New topic
Author

Regular expression

Chris Bro
Greenhorn

Joined: Aug 26, 2004
Posts: 2
Hi all,

I am not verry good in regular expressions and try to remove the character which looks like a square or a rectangle form a String. But I really do not know how to do that. I know that since JDK 1.4 regular expressions are in the package but how do I express this character? It is a non ASCII character and I am not able to paste it here.
I have to send this String using SOAP and during the data encryption the server crashes.
Is there a way to remove all non ASCII characters from a String or has someone
an other adivce?

Thanks in advance

Chris
Tong Chen
Ranch Hand

Joined: Apr 26, 2002
Posts: 1011
try this:

^[-\w`~!@#$%^&*\(\)+={}|\[\]\\:";'<>?,.\/ ]*$

This makes sure string only contains characters that appear on keyboard. Extended characters (such as those with ascii values above 127), will not pass the test.


Tong Chen (Seattle USA)<br />SCJP,SCWCD,SCDJWS,IBM XML,MCP.NET,MCAD.NET,MCSD.NET
Chris Bro
Greenhorn

Joined: Aug 26, 2004
Posts: 2
Hi Tong,

first thank you for your fast reply. Could you please give me a little example how to embed this in Java? As I mentioned I never done this in Java before and unfortunately it is very urgent.

Thanks

Chris
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
What are regular expressions?

A regular expression, also known as a regex or regexp, is a string whose pattern (template) describes a set of strings. The pattern determines what strings belong to the set, and consists of literal characters and metacharacters, characters that have special meaning instead of a literal meaning. The process of searching text to identify matches�strings that match a regex's pattern�is pattern matching.

Java's java.util.regex package supports pattern matching via its Pattern, Matcher, and PatternSyntaxException classes:


Pattern objects, also known as patterns, are compiled regexes

Matcher objects, or matchers, are engines that interpret patterns to locate matches in character sequences, objects whose classes implement the java.lang.CharSequence interface and serve as text sources

PatternSyntaxException objects describe illegal regex patterns


Code example



Run
java RegexDemo apple applet


Result
Regex = apple
Text = applet

Found apple
starting at index 0 and ending at index 5


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
 
jQuery in Action, 2nd edition
 
subject: Regular expression