java, regex and specials characters - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

java, regex and specials characters

zeloszelos Posts: 3Member
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

Comments

  • moomoo Posts: 147Member
    : 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

    [code]
    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());
    [/code]

    greetings
    mo
Sign In or Register to comment.