Results 1 to 6 of 6

Thread: Erase end of a string

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Erase end of a string

    Hello,
    I want to erase the end of a string, but I can not find the correct regular expressions. I have done this
    Code:
    String foo;
    foo = "187zjkhjklhdc18_ & foo = 453";
    And I want to delete everything after the sign "="
    I tried the following
    Code:
    foo = foo.replaceAll("^ *=", "");
    foo = foo.replaceAll("^ *= *\ w* *", "");
    but it does not work. On the other hand, symbols \ or s \ w causes a compilation error, how to make him recognize the regex? Thank you in advance for your help.

  2. #2
    Join Date
    Dec 2009
    Posts
    192

    Re: Erase end of a string

    Hello,
    Even i had a similar kind of a problem, but then I found a way to erase my end of line. I want the index to the character '=', then I take the substring starting at that index, then I replace the empty string.
    Code:
    int ind = foo.indOf("=");
    String tp = foo.substring(Index);
    foo = foo.replaceAll(tp, "");
    By cons, can someone tell me how or why at least the symbols of regular expressions of type \ s or \ w does not work. An explanation will be appreciated.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Erase end of a string

    Hi,
    You seem to use your regex as a string that you'll read in a method, so I think we need to double "\" and put some other characters before, whenever I have problems with regex is due to something the same. It's pretty horrible but it must give something like that:
    Code:
    foo = foo.replaceAll("^ \\*=", "");
    foo = foo.replaceAll("^ \\*= \\*\\w\\* \\*", "");
    Try at least to double the \ before the w and s. Then if you look just separated from the equals sign, why not instead use a split method, knowing that you know the character of separation. You would do something like:
    Code:
    foo = foo.split("=")[0];
    And that's how you recover that part of your string before the "=". The method is split in my opinion much easier to use, if you're looking for is just to take the part of the string placed before the "=".

  4. #4
    Join Date
    Nov 2009
    Posts
    356

    Re: Erase end of a string

    Hi,
    To erase what is after the = simplest would be:
    Code:
    str.replaceAll("^(.*?=).*", "$1");
    As for the \ is a special character in java when you use the quotes he used to give a special meaning to the character who follows the example \ n is newline, \ t a tab. If you want to use \ in a regular expression it you must do so "\ \" for it to be interpreted as a \ normal.

  5. #5
    Join Date
    Dec 2009
    Posts
    178

    Re: Erase end of a string

    Hello,
    I actually did not realize it was double \ \. But my regex was not right with either. I do not understand the regex you suggested. It actually deletes everything after the equals sign, but I want to erase all the blanks before = the = and everything after the = to retrieve only the word.
    Code:
    eg with "foo = 46815rze" I want to retrieve only "foo".
    Can you explain a little your regex? or give me a link to a tutorial or courses on the use of regex? Thank you in advance

  6. #6
    Join Date
    Nov 2009
    Posts
    518

    Re: Erase end of a string

    Hi,
    If you also want to remove the = and the previous space must be:
    str.replaceAll("^(.*?) *=.*", "$1");
    The $ 1 represents the first group in parenthesis in the regex. So I replace the whole line with the content in parentheses. After the * is used to limit its greed. Indeed by default * consumes as many characters as possible, as it does not make the regex invalid. Unlike the *? consumes the minimum of characters needed to make valid regex
    Code:
    String str = "foo = 18do = 25u";
    str.replaceAll("^(.*) *=.*", "$1")   / / returns "foo = 18do"
    str.replaceAll("^(.*?) *=.*", "$1")   / / return "foo"

Similar Threads

  1. Erase an SSD before returning it
    By ADALBERTO in forum Portable Devices
    Replies: 6
    Last Post: 03-07-2010, 06:06 AM
  2. How to use an erase() in C++
    By Gaauge in forum Software Development
    Replies: 5
    Last Post: 12-03-2010, 04:31 PM
  3. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM
  4. How can i erase a DVR tape
    By Kamran in forum Windows Software
    Replies: 3
    Last Post: 28-05-2009, 11:37 AM
  5. How Can I erase CD-RW & DVD-RW
    By BoanHed in forum Windows XP Support
    Replies: 3
    Last Post: 10-03-2007, 08:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,642,726.54490 seconds with 17 queries