Results 1 to 8 of 8
  1. #1
    A.n.H is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Question breaking up a string, a regex problem!!

    Hi there,

    How are you all doing?

    I am writing a simple compiler, now I want to break up the source code I have (which is of type string) into tokens, for example if i have something like this

    Java Code:
    int main()
    {
         test.callBreak();
    }
    the broken up string should be like this

    Java Code:
    int
    main
    (
    )
    {
    test
    .
    callBreak
    (
    )
    ;
    }
    or in other words I want to break up every WORD in the string along with any word that has . ( ) ; in the middle (like test.callBreak(); ).

    I tired StringTokenizer, but it only broke up whole words, and left things like main(), intact, also StringTokenizer is kinda outdated and I rather not use it, after searching around I found out that i could use split(regex), i tried everything but nothing seemed to work!!!

    anyone here has some ideas?

    thanks

  2. #2
    A.n.H is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    hey,

    just wanted to tell u guys that i found a way to fix my problem using StringTokenizer, but if anyone here has any ideas on how to do this using regex feel free to help ;)

  3. #3
    personal's Avatar
    personal is offline Member
    Join Date
    May 2010
    Location
    NY
    Posts
    9
    Rep Power
    0

    Default

    here is some regex /[a-zA-Z]+|\W/ try that with a split, it works for your simple example. You'll have to ignore spaces though, if I can figure out how to remove the spaces I'll post

  4. #4
    A.n.H is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by personal View Post
    here is some regex /[a-zA-Z]+|\W/ try that with a split, it works for your simple example. You'll have to ignore spaces though, if I can figure out how to remove the spaces I'll post
    for some reason I always get "illegal escape character" with /s, /S, /W etc... so I couldnt use your regex, don't know why since i read the documentation and some tutorials about regex and they should work fine!!!

    you have any idea why so that i may test what you suggested, here is what i wrote

    [CODE]
    src.split("[a-zA-Z]+|\W");
    [\CODE]

  5. #5
    personal's Avatar
    personal is offline Member
    Join Date
    May 2010
    Location
    NY
    Posts
    9
    Rep Power
    0

    Default hmm

    do you need to escape the \ so \\W?

    i wrote it in ruby, since its much faster to test out. its standard regex

  6. #6
    A.n.H is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    just learned regex a few days ago i still have a long way to go to fully master it :P

    anyways i just tried your regex, it didnt work it just posts the same input with no changes

    here is what i wrote src.split("[a-zA-Z]+|\\W");

    thanks in advance =)

  7. #7
    personal's Avatar
    personal is offline Member
    Join Date
    May 2010
    Location
    NY
    Posts
    9
    Rep Power
    0

    Default

    sorry i was using ruby's scan method with that pattern. java doesn't seem to have something like that.

  8. #8
    A.n.H is offline Member
    Join Date
    Dec 2009
    Posts
    7
    Rep Power
    0

    Default

    thats alright, as i said i managed to fix this problem with StringTokenizer, i don't know why its not supported no more, its so helpful...

Similar Threads

  1. breaking up a string, a regex problem!!
    By A.n.H in forum Advanced Java
    Replies: 0
    Last Post: 05-17-2010, 04:03 PM
  2. Regex problem
    By Nimyz in forum Advanced Java
    Replies: 4
    Last Post: 05-14-2010, 08:17 AM
  3. Breaking up of array
    By agarwal_srushti in forum New To Java
    Replies: 3
    Last Post: 09-27-2009, 08:03 PM
  4. regex problem - allowing optional space
    By Norm in forum Advanced Java
    Replies: 26
    Last Post: 10-06-2008, 05:38 PM
  5. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 07:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •