-1

I am trying to remove following set of characters from a string if any of them is trailing character

Removing the endings “'”, “--”, “-”, “'s”, “ly”, “ed”, “ing”, “ness”, “)“, “_”, “;”, “?”, “!”, “,”, “:”

I am doing following

value.replaceAll("(ly|!|\\?|')+$","");

But if string is this !lovely?! hi!?' it is giving me !lovely?! hi

I was expecting it to be !lovely?! hi!?

I would really appreciate any help.

erickson
  • 265,237
  • 58
  • 395
  • 493
user2769614
  • 247
  • 3
  • 6
  • 23
  • 1
    You are mixing up grouping `(...)` (or even non-capture grouping `(?:...)`) with character class `[...]`. `value.replaceAll("(ly|!|\?|')+$","");` should work. (Note also you need to escape the question mark as `\?`, as it has special significance in a regular expression.). Also, if you didn't want to remove multiple suffixes, `+` is extra. – Amadan Dec 07 '16 at 04:42
  • 1
    Your call to `replaceAll` does not include all the endings you mentioned. In addition, it appears that you want to remove endings from the end, and keep doing so as long as you find new tokens. This sounds like the job for a parser. – Tim Biegeleisen Dec 07 '16 at 04:44
  • @Amadan I tried your suggestion and edited the question – user2769614 Dec 07 '16 at 04:50
  • 1
    `+` means "repeat 1 or more times". If you just want to strip a single suffix, remove the `+`. – Amadan Dec 07 '16 at 04:50

2 Answers2

0

Match first If the String is ending with your options or not like below -

if (value.endsWith("\'") || value.endsWith("--") || value.endsWith("-"))

and so on your options.

and then remove last character from the String in this if only like this -

value = value.substring(0, value.length()-1);

check below sample code to understand better -

String value = "!lovely?! hi!?'";
if (value.endsWith("\'") || value.endsWith("--") || value.endsWith("-")) {
   value = value.substring(0, value.length()-1);
}
System.out.println(value);

If you want to use Java Regex for comparison then use in this manner -

if(value.matches(".*[' -- - ly ]$"))
  1. .* for starts with more than one characters.
  2. [' -- - ly] gives you actual comparison of your options. provide your options with spaces between each character.
  3. $ for String ends with any of them.

In this manner your can use Regex for comparison of String.

More specifically you can use this best way if you want to remove last one,two,three and four characters from the String.

//provide only one character String that you want to check and remove
if (value.matches(".*[' - ?]$")){
        value = value.substring(0, value.length()-1);
}

//provide two character String that you want to check and remove
if (value.matches(".*[-- ly ab cd ef]$")){
        value = value.substring(0, value.length()-2);
}

//provide three character String that you want to check and remove
if (value.matches(".*[-- lyy abc def ghi]$")){
        value = value.substring(0, value.length()-3);
}

//provide four character String that you want to check and remove
if (value.matches(".*[-- lyyy abcd efgh ijkl]$")){
        value = value.substring(0, value.length()-4);
}
ketan
  • 2,732
  • 11
  • 34
  • 80
  • Wouldn't that be too many conditions for entire set of characters? is it possible with regex? – user2769614 Dec 07 '16 at 04:54
  • @user2769614- yes I'm trying that one only. Give me couple of minutes. – ketan Dec 07 '16 at 04:55
  • @user2769614- see edit for JAVA Regex. you want to compare more than one character at the end of string and In above code I'm removing only last character. please keep in mind this thing. – ketan Dec 07 '16 at 05:14
  • @user2769614 This is not correct. You are looking whole string is pattern match or not. If match you are removing from last digit of value. – newuserua_ext Dec 07 '16 at 05:44
  • @newuserua_ext- see last four if statements into that I'm specifically comparing and removing one,two,three and four characters respectively. – ketan Dec 07 '16 at 05:47
0

Use this Pattern :

var patt = ([-?]|(ing|--|ly|ness|ed))(?=\s);
var result = str.replace(patt,"");

Note : you must add other options inside patt.

Ehsan
  • 12,655
  • 3
  • 25
  • 44