I am having below String value, in that how can I find the only this four specified special character like [],:,{},-() (square bracket, curly bracket, hyphen and colon) in a given String.
String str = "[1-10],{10-20},dhoni:kholi";
Kindly help me as I am new to Java.
I think you can use regular expression like this.
class MyRegex
{
public static void main (String[] args) throws java.lang.Exception
{
String str = "[1-10],{10-20},dhoni:kholi";
String text = str.replaceAll("[a-zA-Z0-9]",""); // replacing all numbers and alphabets with ""
System.out.print(text); // result string
}
}
Hope this will help you.
If it is only characters that you want to check then you can use String.replaceAll method with regular expression
System.out.println("[Hello {}:-,World]".replaceAll("[^\\]\\[:\\-{}]", ""));
Related
I am trying split a string based on regular expression which contains "[.,?!]+'" all these characters including a single space but splitting is not happening?
Here's my class:
public class splitStr {
public static void main(String[] args) {
String S="He is a very very good boy, isn't he?";
S.trim();
if(1<=S.length() && S.length()<=400000){
String delim ="[ .,?!]+'";
String []s=S.split(delim);
System.out.println(s.length);
for(String d:s)
{
System.out.println(d);
}
}
}
}
The reason it's not working is because not all the delimiters are within the square brackets.
String delim ="[ .,?!]+'"; // you wrote this
change to this:
String delim ="[ .,?!']";
Do the characters +, ', [ and ] must be part of the split?
I'm asking this because plus sign and brackets have special meaning in regular expressions, and if you want them to be part of the match, they must be escaped with \
So, if you want an expression that includes all these characters, it should be:
delim = "[\\[ .,\\?!\\]\\+']"
Note that I had to write \\ because the backslash needs to be escaped inside java strings. I'm also not sure if ? and + need to be escaped because they're inside brackets (test it with and without backslashes before them)
I'm not in a front of a computer right now, so I haven't tested it, but I believe it should work.
import java.util.*;
import java.util.stream.Collectors;
public class StringToken {
public static void main(String[] args) {
String S="He is a very very good boy, isn't he?";
S.trim();
if(1<=S.length() && S.length()<=400000){
String delim = "[ .,?!']";
String []s=S.split(delim);
List<String> d = Arrays.asList(s);
d= d.stream().filter(item-> (item.length() > 0)).collect(Collectors.toList());
System.out.println(d.size());
for(String m:d)
{
System.out.println(m);
}
}
}
}
I am trying to write a code to find the special characters in a java string.
Special characters are a-zA-Z.?#;'#~!£$%^&*()_+-=¬`,./<>
Please help me to understand and write how can I implement this.
Thank you
You can create a char array from a String.
String string = "test";
char[] charArray = string.toCharArray();
Then you can loop through all the characters:
for(char character: charArray){
//check if the character is special and do something with it, like adding it to an List.
}
You can use a Scanner to find the invalid characters in your String:
/* Regex with all character considered non-special */
public static final String REGULAR_CHARACTERS = "0-9a-z";
public static String specialCharacters(String string) {
Scanner scanner = new Scanner(string);
String specialCharacters= scanner.findInLine("[^" + REGULAR_CHARACTERS + "]+");
scanner.close();
return specialCharacters;
}
The findInLine returns a String with all characters not included in the constant (all special characters). You need to setup the constant with all the characters that you consider non-special.
Alternatively, if you want to setup only the characters you want to find, you can modify the example above with:
public static final String SPECIAL_CHARACTERS = "a-zA-Z.?#;'#~!£$%^&*()_+-=¬`,./<>";
....
String specialCharacters= scanner.findInLine("[" + SPECIAL_CHARACTERS + "]+");
....
The characters used in the constants need to be scaped as usual for regular expressions.
For example, to add the ] character, you need to use \\]
I want to split a file with a pipe character on a string like number|twitter|abc.. in the mapper.
It is a long string. But it doesn't recognize pipe delimiter when I do:
String[] columnArray = line.split("|");
If I try to split it with a space like line.split(" "), it works fine so I don't think there is a problem with it recognizing characters.
Is there any other character that can look like pipe? Why doesn't split recognize the | character?
As shared in another answer
"String.split expects a regular expression argument. An unescaped | is parsed as a regex meaning "empty string or empty string," which isn't what you mean."
https://stackoverflow.com/a/9808719/2623158
Here's a test example.
public class Test
{
public static void main(String[] args)
{
String str = "test|pipe|delimeter";
String [] tmpAr = str.split("\\|");
for(String s : tmpAr)
{
System.out.println(s);
}
}
}
String.split takes a regular expression (as the javadoc states), and "|" is a special character in regular expressions. try "[|]" instead.
g.:
String string="Marc Louie, Garduque Bautista";
I want to check if a string contains only words, a comma and spaces. i have tried to use regex and the closest I got is this :
String pattern = "[a-zA-Z]+(\\s[a-zA-Z]+)+";
but it doesnt check if there is a comma in there or not. Any suggestion ?
You need to use the pattern
^[A-Za-z, ]++$
For example
public static void main(String[] args) throws IOException {
final String input = "Marc Louie, Garduque Bautista";
final Pattern pattern = Pattern.compile("^[A-Za-z, ]++$");
if (!pattern.matcher(input).matches()) {
throw new IllegalArgumentException("Invalid String");
}
}
EDIT
As per Michael's astute comment the OP might mean a single comma, in which case
^[A-Za-z ]++,[A-Za-z ]++$
Ought to work.
Why not just simply:
"[a-zA-Z\\s,]+"
Use this will best
"(?i)[a-z,\\s]+"
If you mean "some words, any spaces and one single comma, wherever it occurs to be" then my feeling is to suggest this approach:
"^[^,]* *, *[^,]*$"
This means "Start with zero or more characters which are NOT (^) a comma, then you could find zero or more spaces, then a comma, then again zero or more spaces, then finally again zero or more characters which are NOT (^) a comma".
To validate String in java where No special char at beginning and end but may have some special char in between.
String strRGEX = "^[a-zA-Z0-9]+([a-zA-Z0-9-/?:.,\'+_\\s])+([a-zA-Z0-9])$";
String toBeTested= "TesADAD2-3t?S+s/fs:fds'f.324,ffs";
boolean testResult= Pattern.matches(strRGEX, toBeTested);
System.out.println("Test="+testResult);
I have strings like:
Alian 12WE
and
ANI1451
Is there any way to replace all the numbers (and everything after the numbers) with an empty string in JAVA?
I want the output to look like this:
Alian
ANI
With a regex, it's pretty simple:
public class Test {
public static String replaceAll(String string) {
return string.replaceAll("\\d+.*", "");
}
public static void main(String[] args) {
System.out.println(replaceAll("Alian 12WE"));
System.out.println(replaceAll("ANI1451"));
}
}
You could use a regex to remove everyting after a digit is found - something like:
String s = "Alian 12WE";
s = s.replaceAll("\\d+.*", "");
\\d+ finds one or more consecutive digits
.* matches any characters after the digits
Use Regex
"Alian 12WE".split("\\d")[0] // Splits the string at numbers, get the first part.
Or replace "\\d.+$" with ""