Say I have a string like this in java:
"this is {my string: } ok"
Note, there can be any number of white spaces in between the various characters. How do I check the above string to see if it contains just the substring:
"{my string: }"
Many thanks!
If you are looking to see if a String contains another specific sequence of characters then you could do something like this :
String stringToTest = "blah blah blah";
if(stringToTest.contains("blah")){
return true;
}
You could also use matches. For a decent explanation on matching Strings I would advise you check out the Java Oracle tutorials for Regular Expressions at :
http://docs.oracle.com/javase/tutorial/essential/regex/index.html
Cheers,
Jamie
If you have any number of white space between each character of your matching string, I think you are better off removing all white spaces from the string you are trying to match before the search. I.e. :
String searchedString = "this is {my string: } ok";
String stringToMatch = "{my string: }";
boolean foundMatch = searchedString.replaceAll(" ", "").contains(stringToMatch.replaceAll(" ",""));
Put it all into a string variable, say s, then do s.contains("{my string: }); this will return true if {my string: } is in s.
For this purpose you need to use String#contains(CharSequence).
Note, there can be any number of white spaces in between the various
characters.
For this purpose String#trim() method is used to returns a copy of the string, with leading and trailing whitespace omitted.
For e.g.:
String myStr = "this is {my string: } ok";
if (myStr.trim().contains("{my string: }")) {
//Do something.
}
The easiest thing to do is to strip all the spaces from both strings.
return stringToSearch.replaceAll("\s", "").contains(
stringToFind.replaceAll("\s", ""));
Look for the regex
\{\s*my\s+string:\s*\}
This matches any sequence that contains
A left brace
Zero or more spaces
'my'
One or more spaces
'string:'
Zero or more spaces
A right brace
Where 'space' here means any whitespace (tab, space, newline, cr)
Related
I know how to remove duplicated characters from a String and keeping the first occurrences without regex:
String method(String s){
String result = "";
for(char c : s.toCharArray()){
result += result.contains(c+"")
? ""
: c;
}
return result;
}
// Example input: "Type unique chars!"
// Output: "Type uniqchars!"
I know how to remove duplicated characters from a String and keeping the last occurrences with regex:
String method(String s){
return s.replaceAll("(.)(?=.*\\1)", "");
}
// Example input: "Type unique chars!"
// Output: "Typnique chars!"
As for my question: Is it possible, with a regex, to remove duplicated characters from a String, but keep the first occurrences instead of the last?
As for why I'm asking: I came across this codegolf answer using the following function (based on the first example above):
String f(char[]s){String t="";for(char c:s)t+=t.contains(c+"")?"":c;return t;}
and I was wondering if this can be done shorter with a regex and String input. But even if it's longer, I'm just curious in general if it's possible to remove duplicated characters from a String with a regex, while keeping the first occurrences of each character.
It is not the shortest option, and does not only involve a regex, but still an option. You may reverse the string before running the regex you have and then reverse the result back.
public static String g(StringBuilder s){
return new StringBuilder(
s.reverse().toString()
.replaceAll("(?s)(.)(?=.*\\1)", ""))
.reverse().toString();
}
See the online Java demo
Note I suggest adding (?s) (= Pattern.DOTALL inline modifier flag) to the regex so as . could match any symbol including a newline (a . does not match all line breaks by default).
I have a string with \r\n, \r, \n or \" characters in it. How can I replace them faster?
What I already have is:
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replace("\\r\\n", "\n").replace("\\r", "").replace("\\n", "").replace("\\", ""));
But my code does not look beautiful enough.
I found on the Internet something like:
replace("\\r\\n|\\r|\\n|\\", "")
I tried that, but it didn't work.
You can wrap it in a method, put /r/n, /n and /r in a list. iterate the list and replace all such characters and return the modified string.
public String replaceMultipleSubstrings(String original, List<String> mylist){
String tmp = original;
for(String str: mylist){
tmp = tmp.replace(str, "");
}
return tmp;
}
Test:
mylist.add("\\r");
mylist.add("\\r\\n");
mylist.add("\\n");
mylist.add("\\"); // add back slash
System.out.println("original:" + s);
String x = new Main().replaceMultipleSubstrings(s, mylist);
System.out.println("modified:" + x);
Output:
original:Kerner\r\n kyky\r hihi\n \"
modified:Kerner kyky hihi "
I don't know if your current replacement logic be correct, but it says now that either \n, \r, or \r\n gets replaced with empty string, and backslash also gets replaced with empty string. If so, then you can try the following regex replace all:
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\r|\\n|\\r\\n|\\\\", ""));
One problem I saw with your attempt is that you are calling replace(), not replaceAll(), so it would only do a single replacement and then stop.
String.replaceAll() can be used, in your question you tried to use String.replace() which does not interpret regular expressions, only plain replacement strings...
You also need to escape the \\ again, i.e. \\\\ instead of \\
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\\\r|\\\\n|\\\\\"", ""));
Output
Kerner kyky hihi
Note the differences between String.replaceAll() and String.replace()
String.replaceAll()
Replaces each substring of this string that matches the given regular
expression with the given replacement.
String.replace()
Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence.
Use a regular expression if you want to do all the replaces in one go.
http://www.javamex.com/tutorials/regular_expressions/search_replace.shtml
I have this code
public String StripText(String name){
String stripped = name.replaceAll("/:!##$%^&*()<>+?\"{}[]=`~;", "");
return stripped;
}
which doesn't work. I want it to return a string, erasing occurrences of characters like "/" ":" "!" "#" and so on.
e.g. If I give it a string "puppy:)love", I want it to return a string containing only "puppylove".
You just need to put all the characters inside a character class.With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets.
String stripped = name.replaceAll("[/:!##$%^&*()<>+?\"{}\\[\\]=`~;]", "");
You also need to escape [, ] characters present inside the character class or it would consider the first ] as an end of charclass.
Example:
String name = "[{puppy:)love}]";
String stripped = name.replaceAll("[/:!##$%^&*()<>+?\"{}\\[\\]=`~;]", "");
System.out.println(stripped);
Output:
puppylove
You need to escape all the special characters or use [] like this [/:!##$%^&*()<>+?\"{}[]=~;]+ in your code
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");
}
I want to remove all Unicode Characters and Escape Characters like (\n, \t) etc. In short I want just alphanumeric string.
For example :
\u2029My Actual String\u2029
\nMy Actual String\n
I want to fetch just 'My Actual String'. Is there any way to do so, either by using a built in string method or a Regular Expression ?
Try
String stg = "\u2029My Actual String\u2029 \nMy Actual String";
Pattern pat = Pattern.compile("(?!(\\\\(u|U)\\w{4}|\\s))(\\w)+");
Matcher mat = pat.matcher(stg);
String out = "";
while(mat.find()){
out+=mat.group()+" ";
}
System.out.println(out);
The regex matches all things except unicode and escape characters. The regex pictorially represented as:
Output:
My Actual String My Actual String
Try this:
anyString = anyString.replaceAll("\\\\u\\d{4}|\\\\.", "");
to remove escaped characters. If you also want to remove all other special characters use this one:
anyString = anyString.replaceAll("\\\\u\\d{4}|\\\\.|[^a-zA-Z0-9\\s]", "");
(I guess you want to keep the whitespaces, if not remove \\s from the one above)