Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular expression

 
Chris Bro
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1011
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Bro
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic