• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regular expression

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1011
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
reply
    Bookmark Topic Watch Topic
  • New Topic