Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Erase end of a string

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2010
Member
 
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.

Reply With Quote
  #2  
Old 03-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 03-02-2010
Member
 
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 "=".
Reply With Quote
  #4  
Old 03-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 03-02-2010
Member
 
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
Reply With Quote
  #6  
Old 03-02-2010
Member
 
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"
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 10:23 PM.