Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By kjkrum

Thread: Dude in replace

  1. #1
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Dude in replace

    I explain problem.

    My program needs to know all numbers, possitive an negative

    I know how to replace all the chars that aren't digits, but i dont know how to do for - that say a number is < 0 dont remove

    Eg:

    String s = " hello, -3World, My name Pedro234"

    then i use

    s.replaceAll("\\D" , " ");


    and the result is :

    3 234

    but not

    -3 234

    this is because \\D remove all that arent a digit, i dont know a form to do it without removing -


    thx to all

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default Re: Dude in replace


  4. #4
    spinter is offline Member
    Join Date
    Oct 2011
    Posts
    18
    Rep Power
    0

    Default Re: Dude in replace

    but it thas what i explai raplace all negative numbers too

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Dude in replace

    You could use String.split() with a regex that matches both positive and negative numbers. Something like "-?[1-9][0-9]*", although I don't remember off the top of my head if the '-' would need to be escaped there.
    Fubarable likes this.
    Get in the habit of using standard Java naming conventions!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Dude in replace

    Quote Originally Posted by kjkrum View Post
    You could use String.split() with a regex that matches both positive and negative numbers. Something like "-?[1-9][0-9]*", although I don't remember off the top of my head if the '-' would need to be escaped there.
    Yes, it would need escaping.

  7. #7
    ojn
    ojn is offline Senior Member
    Join Date
    Sep 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default Re: Dude in replace

    Quote Originally Posted by spinter View Post
    but it thas what i explai raplace all negative numbers too
    Well.. I found one solution through that link. One the first hits actually. :)

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Dude in replace

    And actually, my reasoning was backward... using split with a regex like the one I suggested would give you everything except the numbers you're interested in. You'd want to use that regex with a Matcher.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Char replace
    By susieferrari in forum New To Java
    Replies: 4
    Last Post: 06-08-2011, 03:06 PM
  2. Find and replace
    By ravikanth.29 in forum AWT / Swing
    Replies: 1
    Last Post: 03-14-2011, 10:46 AM
  3. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 04:58 PM
  4. Hello from an old dude
    By fishtoprecords in forum Introductions
    Replies: 4
    Last Post: 06-29-2008, 06:38 AM
  5. Hello from an even older Dude
    By Alan-LB in forum Introductions
    Replies: 10
    Last Post: 06-29-2008, 06:29 AM

Posting Permissions

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