I'm new with regex and just can't find what the regex is to prohibit a backslash.
Thanks for any advice.
EDIT:
I'm using the regex for JTextField, to avoid that the user writes an unvalid input. This regex currently doesnt allow the user to write a space character.
I'm doing this with
String regex = "\\S{1}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
So how I change my regex to prohibit backslash as well?
Based on your example snippet, the following expression should work similarly, but also disallowing the backslash:
String regex = "[^\\s\\\\]{1}";
It is a bit strange that you are looking for a one-char non-space and non-backslash pattern, but I guess you are iterating through and checking for consecutive matches.
I would use the following regex though:
String regex = "[^\\s\\\\]+";
and check whether it matched the whole String (matcher.matches()).
The regex pattern with java is \\\\
String somestring;
somestring = somestring.replaceAll("\\\\", "");
Would remove them. Semantically it equates down to \\ at the regex level, which becomes a literal \ match.
You can also use a Pattern match if you want to just compare, or just use String#contains
String somestring;
if (somestring.contains("\\")) {...}
To test if a string has a backslash:
if (input.matches(".*\\\\.*"))
To remove backslashes from strings:
input = input.replace("\\", "");
Note the use of the non-regex based replace() method.
Related
I was having some problem with regex in Java. I have this regex pattern:
static Pattern parts = Pattern.compile("\\A([91|10|17|21|30].{1,20}\\s){1,5}\\Z");
Then I try to test it with dummy data:
String s = "91448629517150623101408002301";
Matcher testMatcher = parts.matcher(s);
System.out.println(testMatcher.matches());
String s1 = "9143676601715Sep14101310147301";
Matcher testMatcher1 = parts.matcher(s1);
System.out.println(testMatcher1.matches());
The dummy data is in the correct format. However, I not sure why both keep returns me false. Any ideas?
The regular expression you provided might be incorrect for matching the pattern. I think you might want to use \s (A whitespace character) instead of \S (A non-whitespace character).
I suggest you go through the documentation again carefully.
For example,
static Pattern parts = Pattern.compile("\\A([91|10|17|21|30].{1,20}\\S){1,5}\\Z");
How to get ricCode:.ABC from following string.
My matcher is
Matcher matcher = Pattern.compile("ricCode:([A-Za-z]+),$").matcher(str);
String str = "{AMX:{ricCode:.ABC,indexDetailEnable:true,price:20,648.15,netChange:<spanclass="md-down">-41.09</span>,percentChange:<spanclass="md-down">-0.20%</span>,tradeDate:17/04/05,tradeTime:16:40,chartDate:17/04/05,chartTime:16:40}";
What is missing in the regex?
Change your regex from this:
ricCode:([A-Za-z]+),$
to this:
ricCode:([A-Za-z.]+)(?=,)
Your original regex would only allow alphabetic characters after ricCode:, but your example has a period . character. Also you were matching the , character, but this would also include the comma in your match, you dont want this - so I added a positive lookahead for the comma so it looks for it there but does not match it. Finally you had the $ character at the end of your regex which matches the end of the string, you dont want to look for the end of the string immediately after the comma, so I removed it.
It helps to use regexr.com to test out your expression.
I'm sure I'm just overlooking something here...
Is there a simple way to split a String on an explicit character without applying RegEx rules?
For instance, I receive a string with a dynamic delimiter, I know the 5th character defines the delimiter.
String s = "This,is,a,sample";
For this, it's simple to do
String delimiter = String.valueOf(s.charAt(4));
String[] result = s.split(delimiter);
However, when I have a delimiter that's a special RegEx character, this doesn't work:
String s = "This*is*a*sample";
So... is there a way to split the string on an explicit character without trying to apply extra RegEx rules? I feel like I must be missing something pretty simple.
split uses a regular expression as its argument. * is a meta-character used to match zero of more characters in regular expressions, You could use Pattern#quote to avoid interpreting the character
String[] result = s.split(Pattern.quote(delimiter));
You need not to worry about the character type If you use Pattern
Pattern regex = Pattern.compile(s.charAt(4));
Matcher matcher = regex.matcher(yourString);
if (matcher.find()){
//do something
}
You can run Pattern.quote on the delimiter before feeding it in. This will create a string literal and escape any regex specific chars:
delimiter = Pattern.quote(delimiter);
StringUtils.split(s, delimiter);
That will treat the delimiter as just a character, not use it like a regex.
StringUtils is a part of the ApacheCommons library, which is tons of useful methods. It is worth taking a look, could save you some time in the future.
Simply put your delimiter between []
String delimiter = "["+s.charAt(4)+"]";
String[] result = s.split(delimiter);
Since [ ] is the regex matches any characters between [ ]. You can also specify a list of delimiters like [*,.+-]
I am using a regular expression for finding string in between two strings
Code:
Pattern pattern = Pattern.compile("EMAIL_BODY_XML_START_NODE"+"(.*)(\\n+)(.*)"+"EMAIL_BODY_XML_END_NODE");
Matcher matcher = pattern.matcher(part);
if (matcher.find()) {
..........
It works fine for texts but when text contains special characters like newline it's break
You need to compile the pattern such that . matches line terminaters as well. To do this you need to use the DOTALL flag.
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
edit: Sorry, it's been a while since I've had this problem. You'll also have to change the middle regex from (.*)(\\n+)(.*) to (.*?). You need to lazy quantifier (*?) if you have multiple EMAIL_BODY_XML_START_NODE elements. Otherwise the regex will match the start of the first element with the end of the last element rather than having separate matches for each element. Though I'm guessing this is unlikely to be the case for you.
here monitorUrl contains- http://host:8810/solr/admin/stats.jsp
and monitorUrl sometimes can be-- http://host:8810/solr/admin/monitor.jsp
So i want to replace stats.jsp and monitor.jsp to ping
if(monitorUrl.contains("stats.jsp") || monitorUrl.contains("monitor.jsp")) {
trimUrl = monitorUrl.replace("[stats|monitor].jsp", "ping");
}
Anything wrong with the above code. As I get the same value of monitorUrl in trimUrl.
Try using replaceAll instead of replace (and escape the dot as Alan pointed out):
trimUrl = monitorUrl.replaceAll("(stats|monitor)\\.jsp", "ping");
From the documentation:
replaceAll
public String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement.
Note: You may also want to consider matching only after a / and checking that it is at the end of the line by using $ at the end of your regular expression.
I think this is what you're looking for:
trimUrl = monitorUrl.replaceAll("(?:stats|monitor)\\.jsp", "ping");
Explanation:
replaceAll() treats the first argument as a regex, while replace() treats it as a literal string.
You use parentheses, not square brackets, to group things. (?:...) is the non-capturing form of group; you should use the capturing form - (...) - only when you really need to capture something.
. is a metacharacter, so you need to escape it if you want to match a literal dot.
And finally, you don't have to check for the presence of the sentinel string separately; if it's not there, replaceAll() just returns the original string. For that matter, so does replace(); you could also have done this:
trimUrl = monitorUrl.replace("stats.jsp", "ping")
.replace("monitor.jsp", "ping");
No needs to use regex (also replace() don't use regex).
trimUrl = monitorUrl.replace("stats.jsp", "ping").replace("monitor.jsp", "ping");