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.
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 "=".
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.
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
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"