Fairly new to regular expressions. I want to be able to remove all non-alphanumeric characters besides $.
So for the string like "I am here $today#", the result should be "I am here $today"
I have tried this already with no luck.
[^a-zA-Z\\s$] and [$^a-zA-Z\\s] and [^a-zA-Z$\\s]
String regex = "[^a-zA-Z\\s$]";
String string = "I am here $today#";
string = string.replaceAll(regex, "");
System.out.println(string); // I am here $today
This is working just fine...
This might help, Use:
replaceAll("[^\\w\\s\\$]", "");
\w is short for [a-zA-Z_0-9]
Related
I am working on a project where i need to search for a particular string token and find if this token has the [3:0] format of number, how can i check it? i searched for reference on stack overflow, i could find how to search "{my string }:" in a string like the following:
String myStr = "this is {my string: } ok";
if (myStr.trim().contains("{my string: }")) {
//Do something.
}
But, could not find how to search if a string contains a number in the regular expression, i tried using the following, but it did not work:
String myStr = "this is [3 string: ] ok";
if (myStr.trim().contains("[\\d string: ]")) {
//Do something.
}
Please help!
for "[int:int]" use \\[\\d*:\\d*\\] it's working
You cannot use a regex inside String#contains, instead, use .matches() with a regex.
To match [3 string: ]-like patterns inside larger strings (where string is a literal word string), use a regex like (?s).*\\[\\d+\\s+string:\\s*\\].*:
String myStr = "this is [3 string: ] ok";
if (myStr.matches("(?s).*\\[\\d+\\s+string:\\s*\\].*")) {
System.out.println("FOUND");
}
See IDEONE demo
The regex will match any number of any characters from the start of string with .* (as many as possible) before a [+1 or more digits+1 or more whitespace+string:+0 or more whitespace+]+0 or more any characters up to the end of string.
The (?s) internal modifier makes the dot match newline characters, too.
Note we need .* on both sides because .matches() requires a full string match.
To match [3:3]-like pattern inside larger strings use:
"(?s).*\\[\\d+\\s*:\\s*\\d+\\].*"
See another IDEONE demo
Remove \\s* if whitespace around : is not allowed.
I have this kind of String ('123','12345678') and i would validate it throw a regex.
I have write this code, but i'm not shure it work.
String field = "('123','12345678')";
String regex = "^('\\d{3}','\\d{8}')$";
public void valideField(String field, String regex){
{
if(!field.matches(regex)){
System.out.println("Not validated!");
}
}
Is correct regex or not? You have any suggestions or help?
You need to escape parentheses (using backslashes) as they represent capturing groups in regular expressions:
String regex = "^\\('\\d{3}','\\d{8}'\\)$";
You have to escape the single quotes and parenthesis too with \\' and \\(
The correct regex would be:
String regex = "\\(\\'\\d{3}\\',\\'\\d{8}\\'\\)";
I have a String like <li><font color='#008000'> [INFO]a random user. and I want to eliminate html tags such as <li> and <font> from this String.
I tried to achieve this with String.replaceAll method in Java but it doesn't work...
public static String removeHTMLTags(String original){
String str = original.replaceAll("^<.+>$", "");
return str;
}
Your regex isn't finding a match because the ^ and $ anchors specify that the very first character in the input string must be < and the very last must be >.
Without those anchors, your regex still won't do what you want, however, because quantifiers (such as .+) are by default greedy.
So if your input string was text1 <a href=foo>bar</a> text2, your transformed output would be text1 text2, because the regex would match everything from the first < to the last >.
So in order to stop at the first >, you should make your quantifier non-greedy: .+?.
Remove the ^ and $ and use a reluctant quantifier with the dotall flag (so dot matches newlines too):
public static String removeHTMLTags(String original){
return original.replaceAll("(?s)<.+?>", "");
}
or use a negated character class (which will match newlines)
public static String removeHTMLTags(String original){
return original.replaceAll("<[^>]+>", "");
}
You're transforming a HTML string that might have newline characters as well. DOT doesn't match new line characters in regex. You need to use (?s) (DOTALL) flag with lazy quantifier and without anchors:
String str = original.replaceAll("(?s)<.+?>", "");
Though I must caution you using regex to parse/transform HTML, it can be error prone.
I have a String, which I want to split into parts using delimeter }},{". I have tried using:
String delims="['}},{\"']+";
String field[]=new String[50];
field=subResult.split(delims);
But it is not working :-( do you know, what expression in delims should I use?
Thanks for your replies
A { is a regex meta-character which marks the beginning of a character class. To match a literal { you need to escape it by preceding it with a \\ as:
String delims="}},\\{";
String field[] = subResult.split(delims);
You need not escape the } in your regex as the regex engine infers that it is a literal } as it is not preceded by a opening {. That said there is no harm in escaping it.
See it
If the delimiter is simply }},{ then subResult.split("\\}\\},\\{") should work
String fooo = "asdf}},{bar}},{baz";
System.out.println(Arrays.toString(fooo.split("\\}\\},\\{")));
You should be escaping it.
String.split("\\}\\},\\{");
You could be making it more complex than you need.
String text = "{{aaa}},{\"hello\"}";
String[] field=text.split("\\}\\},\\{\"");
System.out.println(Arrays.toString(field));
Use:
Pattern p = Pattern.compile("[}},{\"]");
// Split input with the pattern
String[] result = p.split(MyTextString);
This seems like a well known title, but I am really facing a problem in this.
Here is what I have and what I've done so far.
I have validate input string, these chars are not allowed :
&%$###!~
So I coded it like this:
String REGEX = "^[&%$###!~]";
String username= "jhgjhgjh.#";
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(username);
if (matcher.matches()) {
System.out.println("matched");
}
Change your first line of code like this
String REGEX = "[^&%$##!~]*";
And it should work fine. ^ outside the character class denotes start of line. ^ inside a character class [] means a negation of the characters inside the character class. And, if you don't want to match empty usernames, then use this regex
String REGEX = "[^&%$##!~]+";
i think you want this:
[^&%$###!~]*
To match a valid input:
String REGEX = "[^&%$##!~]*";
To match an invalid input:
String REGEX = ".*[&%$##!~]+.*";