Reputation: 23
I have a sentence jafjaklf domain1-12-123.eng.abc.com amkfg,fmgsklfgm domain2-134-135.eng.abc.com
. I want to replace the words ending with .eng.abc.com
with "".
I used the regex pattern:
\b(.*\.eng\.abc\.com)\b
But it matches " domain1-12-123.eng.abc.com amkfg,fmgsklfgm domain2-134-135.eng.abc.com"
.
Could anyone help me with the pattern
Upvotes: 2
Views: 501
Reputation: 626816
It seems that the "words" you want to match may contain non-word chars. I suggest matching those parts with a \S
, non-whitespace pattern:
\b\S*\.eng\.abc\.com\b
See the regex demo
Details:
\b
- a word boundary\S*
- 0+ chars other than whitespace\.eng\.abc\.com
- a literal .eng.abc.com
substring\b
- end of word.Do not forget to double the backslashes in the Java string literal.
String s = "jafjaklf domain1-12-123.eng.abc.com amkfg,fmgsklfgm domain2-134-135.eng.abc.com";
String pat = "\\s*\\b\\S*\\.eng\\.abc\\.com\\b";
String res = s.replaceAll(pat ,"");
System.out.println(res);
// => jafjaklf amkfg,fmgsklfgm
Upvotes: 2
Reputation: 593
if(str.matches(".*com.?$") || str.matches(".*abc.?$") || str.matches(".*eng.?$"))
Upvotes: 0
Reputation: 53525
You don't need to use regex, you can use String.endsWith
and String.substring
:
String str = "domain1-12-123.eng.abc.com amkfg,fmgsklfgm domain2-134-135.eng.abc.com";
if (str.endsWith(".eng.abc.com")) {
str = str.substring(0, str.length() - 12);
}
System.out.println(str); // domain1-12-123.eng.abc.com amkfg,fmgsklfgm domain2-134-135
Upvotes: 0