Regex to find last letter in word for any String - java

I have a small problem with Regular Expressions ( regex ).
I want to remove any "T" at the end of each word in the string.
This is the code I am using to display all the words ending with "T".
public static void main (String []args){
String name = "PHYLAURHEIMSMET hello tttttyyuolktttb fedqs jhgjt";
p = Pattern.compile("([a-z0-9]+)?[t]");
m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
}
Thank you for all your help.

i try to remove any "T" at the end of each word
string.replaceAll("T(?!\\S)", "");
This would match all the T's which was present at the end of each word.
OR
string.replaceAll("T(?=\\s|$)", "");
This would match all the t's only if it's follwed by a space or end of the line anchor.
To do a case-insensitive replacement.
string.replaceAll("(?i)T(?!\\S)", "");
DEMO

I would use the \b word-boundary to test if the letter is at the end of the word, you can see the result of replacing t\b with an empty string on this regex101.
I'm not sure if you want to only remove upper case T's, in that case, remove the i all the way to the right.
In Java that would be:
string.replaceAll("T\b", ""); // <-- only upper case T's
string.replaceAll("(?i)T\b", ""); // <-- both t and T's

This is just suggetion why not to use simple String class inbuilt functions something like this :
String name = "PHYLAURHEIMSMET hello tttttyyuolktttb fedqs jhgjt";
if ('t' == name.charAt(name.length() - 1) || 'T' == name.charAt(name.length() - 1)) {
System.out.println("contains last char 't' or 'T'");
}

Related

How can I replace everything between 2nd "." and “:” in java?

Been researching online but haven't been able to find a solution.
I've got the following string '555.8.0.i5:790.2.0.i19:904.1.0:8233.2:' in Java.
Whats the best way I can remove everything from and including the second dot to the colon?
I want the string to end up looking like this: 555.8:790.2:904.1:8233.2:
I saw on another post someone had referenced the second dot with java regex (\d+.\d.) but I'm not sure how to do the trim.
EDIT:
I have tried the following java regex .replaceAll("\\.(.*?):", ":"); but it seems to remove everything from the first dot. Not sure how to get it to trim from the second dot.
In your case, you may use
.replaceAll("(\\.[^:.]+)\\.[^:]+", "$1")
See the regex demo
Details:
(\\.[^:.]+) - Capture group 1 capturing a dot and 1+ chars other than a literal dot and colon
\\. - a literal dot
[^:]+ - 1+ chars other than a colon.
In the replacement pattern, only a $1 backreference to the value captured in Group 1 is used.
Do you have to use regex? Here is a solution using Java:
public static void main(String[] args) {
String myString = "555.8.0.i5:790.2.0.i19:904.1.0:8233.2:";
StringBuilder sb = new StringBuilder();
//Split the string into an array of strings at each colon
String[] stringParts = myString.split(":");
//Loop over each substring
for (String stringPart : stringParts) {
//Find the index of the second dot
int secondDotIndex = stringPart.indexOf('.', 1 + stringPart.indexOf('.', 1));
//If a second dot exists then remove everything after and including the dot
if (secondDotIndex != -1) {
stringPart = stringPart.substring(0, secondDotIndex);
}
//Append each string part and colon back to the final string
sb.append(stringPart);
sb.append(":");
}
System.out.println(sb.toString());
}
The final println prints 555.8:790.2:904.1:8233.2:

Java match whole word in String

I have an ArrayList<String> which I iterate through to find the correct index given a String. Basically, given a String, the program should search through the list and find the index where the whole word matches. For example:
ArrayList<String> foo = new ArrayList<String>();
foo.add("AAAB_11232016.txt");
foo.add("BBB_12252016.txt");
foo.add("AAA_09212017.txt");
So if I give the String AAA, I should get back index 2 (the last one). So I can't use the contains() method as that would give me back index 0.
I tried with this code:
String str = "AAA";
String pattern = "\\b" + str + "\\b";
Pattern p = Pattern.compile(pattern);
for(int i = 0; i < foo.size(); i++) {
// Check each entry of list to find the correct value
Matcher match = p.matcher(foo.get(i));
if(match.find() == true) {
return i;
}
}
Unfortunately, this code never reaches the if statement inside the loop. I'm not sure what I'm doing wrong.
Note: This should also work if I searched for AAA_0921, the full name AAA_09212017.txt, or any part of the String that is unique to it.
Since word boundary does not match between a word char and underscore you need
String pattern = "(?<=_|\\b)" + str + "(?=_|\\b)";
Here, (?<=_|\b) positive lookbehind requires a word boundary or an underscore to appear before the str, and the (?=_|\b) positive lookahead requires an underscore or a word boundary to appear right after the str.
See this regex demo.
If your word may have special chars inside, you might want to use a more straight-forward word boundary:
"(?<![^\\W_])" + Pattern.quote(str) + "(?![^\\W_])"
Here, the negative lookbehind (?<![^\\W_]) fails the match if there is a word character except an underscore ([^...] is a negated character class that matches any character other than the characters, ranges, etc. defined inside this class, thus, it matches all characters other than a non-word char \W and a _), and the (?![^\W_]) negative lookahead fails the match if there is a word char except the underscore after the str.
Note that the second example has a quoted search string, so that even AA.A_str.txt could be matched well with AA.A.
See another regex demo

java regex replaceAll with negated groups

I'm trying to use the String.replaceAll() method with regex to only keep letter characters and ['-_]. I'm trying to do this by replacing every character that is neither a letter nor one of the characters above by an empty string.
So far I have tried something like this (in different variations) which correctly keeps letters but replaces the special characters I want to keep:
current = current.replaceAll("(?=\\P{L})(?=[^\\'-_])", "");
Make it simplier :
current = current.replaceAll("[^a-zA-Z'_-]", "");
Explanation :
Match any char not in a to z, A to Z, ', _, - and replaceAll() method will replace any matched char with nothing.
Tested input : "a_zE'R-z4r#m"
Output : a_zE'R-zrm
You don't need lookahead, just use negated regex:
current = current.replaceAll("[^\\p{L}'_-]+", "");
[^\\p{L}'_-] will match anything that is not a letter (unicode) or single quote or underscore or hyphen.
Your regex is too complicated. Just specify the characters you want to keep, and use ^ to negate, so [^a-z'_-] means "anything but these".
public class Replacer {
public static void main(String[] args) {
System.out.println("with 1234 &*()) -/.,>>?chars".replaceAll("[^\\w'_-]", ""));
}
}
You can try this:
String str = "Se#rbi323a`and_Eur$ope#-t42he-[A%merica]";
str = str.replaceAll("[\\d+\\p{Punct}&&[^-'_\\[\\]]]+", "");
System.out.println("str = " + str);
And it is the result:
str = Serbia'and_Europe-the-[America]

Remove all punctuation from the end of a string

Examples:
// A B C. -> A B C
// !A B C! -> !A B C
// A? B?? C??? -> A? B?? C
Here's what I have so far:
while (endsWithRegex(word, "\\p{P}")) {
word = word.substring(0, word.length() - 1);
}
public static boolean endsWithRegex(String word, String regex) {
return word != null && !word.isEmpty() &&
word.substring(word.length() - 1).replaceAll(regex, "").isEmpty();
}
This current solution works, but since it's already calling String.replaceAll within endsWithRegex, we should be able to do something like this:
word = word.replaceAll(/* regex */, "");
Any advice?
I suggest using
\s*\p{Punct}+\s*$
It will match optional whitespace and punctuation at the end of the string.
If you do not care about the whitespace, just use \p{Punct}+$.
Do not forget that in Java strings, backslashes should be doubled to denote literal backslashes (that must be used as regex escape symbols).
Java demo
String word = "!Words word! ";
word = word.replaceAll("\\s*\\p{Punct}+\\s*$", "");
System.out.println(word); // => !Words word
You can use:
str = str.replaceFirst("\\p{P}+$", "");
To include space also:
str = str.replaceFirst("[\\p{Space}\\p{P}]+$", "")
how about this, if you can take a minor hit in efficiency.
reverse the input string
keep removing characters until you hit an alphabet
reverse the string and return
I have modified the logic of your method
public static boolean endsWithRegex(String word, String regex) {
return word != null && !word.isEmpty() && word.matches(regex);
}
and your regex is : regex = ".*[^a-zA-Z]$";

How to replace last letter to another letter in java using regular expression

i have seen to replace "," to "." by using ".$"|",$", but this logic is not working with alphabets.
i need to replace last letter of a word to another letter for all word in string containing EXAMPLE_TEST using java
this is my code
Pattern replace = Pattern.compile("n$");//here got the real problem
matcher2 = replace.matcher(EXAMPLE_TEST);
EXAMPLE_TEST=matcher2.replaceAll("k");
i also tried "//n$" ,"\n$" etc
Please help me to get the solution
input text=>njan ayman
output text=> njak aymak
Instead of the end of string $ anchor, use a word boundary \b
String s = "njan ayman";
s = s.replaceAll("n\\b", "k");
System.out.println(s); //=> "njak aymak"
You can use lookahead and group matching:
String EXAMPLE_TEST = "njan ayman";
s = EXAMPLE_TEST.replaceAll("(n)(?=\\s|$)", "k");
System.out.println("s = " + s); // prints: s = njak aymak
Explanation:
(n) - the matched word character
(?=\\s|$) - which is followed by a space or at the end of the line (lookahead)
The above is only an example! if you want to switch every comma with a period the middle line should be changed to:
s = s.replaceAll("(,)(?=\\s|$)", "\\.");
Here's how I would set it up:
(?=.\b)\w
Which in Java would need to be escaped as following:
(?=.\\b)\\w
It translates to something like "a character (\w) after (?=) any single character (.) at the end of a word (\b)".
String s = "njan ayman aowkdwo wdonwan. wadawd,.. wadwdawd;";
s = s.replaceAll("(?=.\\b)\\w", "");
System.out.println(s); //nja ayma aowkdw wdonwa. wadaw,.. wadwdaw;
This removes the last character of all words, but leaves following non-alphanumeric characters. You can specify only specific characters to remove/replace by changing the . to something else.
However, the other answers are perfectly good and might achieve exactly what you are looking for.
if (word.endsWith("char oldletter")) {
name = name.substring(0, name.length() - 1 "char newletter");
}

Categories