Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
java, regex and specials characters Posted by zelos on 21 Jun 2005 at 3:20 AM
Hi everybody!

I have to read each line from a texte file. Each line looks like this:
Napoleon | Caesar | Mickey | Lucky Luke
Next, I have to convert this String to get the following Array:
name[0]="Napoleon"
name[1]="Caesar"
name[2]="Mickey"
name[3]="Lucky Luke"

Here's the code:

Pattern p = Pattern.compile("Napoleon | Caesar | Mickey | Lucky Luke");
Matcher m = p.matcher("\\u7C?\\u20\\p{Alnum}*\\u20\\u7C?");

System.out.println("(" + m.groupCount() +" names) ");

\\u7C may represent character | in ASCII(hex)
\\u20 may represent character SPACE in ASCII(hex)

And the result:

0 names

Any idea? I'm getting crazy!!!!!

Thanks for your help,
Zelos
Report
Re: java, regex and specials characters Posted by moo on 28 Jun 2005 at 5:51 AM
: Hi everybody!
:
: I have to read each line from a texte file. Each line looks like this:
: Napoleon | Caesar | Mickey | Lucky Luke
: Next, I have to convert this String to get the following Array:
: name[0]="Napoleon"
: name[1]="Caesar"
: name[2]="Mickey"
: name[3]="Lucky Luke"
:
: Here's the code:
:
: Pattern p = Pattern.compile("Napoleon | Caesar | Mickey | Lucky Luke");
: Matcher m = p.matcher("\\u7C?\\u20\\p{Alnum}*\\u20\\u7C?");
:
: System.out.println("(" + m.groupCount() +" names) ");
:
: \\u7C may represent character | in ASCII(hex)
: \\u20 may represent character SPACE in ASCII(hex)
:
: And the result:
:
: 0 names
:
: Any idea? I'm getting crazy!!!!!
:
: Thanks for your help,
: Zelos
:


hi,
i have absolutely no idea what you are trying to do, the code above makes no sence to me. you compile a regular expression and try if it matches a character sequence, i think you switched this.
for instance the regular expression "[01]+[bB]" matches '0101b' or '0101011100B' etc. so if you like to test whether 'asdf' matches "[01]+[bB]" you will have to code the following

  Pattern binNo = Pattern.compile("[01]+[bB]");
  Matcher doesMatch = binNo.matcher("01001111b");
  Matcher noMatch = binNo.matcher("asdf");

  System.out.println("doesMatch: "+doesMatch.matches());
  System.out.println("noMatch:   "+noMatch.matchers());


greetings
mo



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.