Regex of base classes - java

I am trying to create a hexadecimal calculator but I have a problem with the regex.
Basically, I want the string to only accept 0-9, A-E, and special characters +-*_
My code keeps returning false no matter how I change the regex, and the adding the asterisk is giving me a PatternSyntaxException error.
public static void main(String[] args) {
String input = "1A_16+2B_16-3C_16*4D_16";
String regex = "[0-9A-E+-_]";
System.out.println(input.matches(regex));
}
Also whenever I add the * as part of the regex it gives me this error:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 9
[0-9A-E+-*_]+
^

You need to match more than one character with your regex. As it currently stands you only match one character.
To match one or more characters add a + to the end of the regex
[0-9A-E+-_]+
Also to match a * just add a star in the brackets so the final regex would be
[0-9A-E+\\-_*]+
You need to escape the - otherwise the regex thinks you want to accept all character between + and _ which is not what you want.

You regex is OK there should be no exceptions, just add + at the end of regex which means one or more characters like those in brackets, and it seems you wanted * as well
"[0-9A-E+-_]+"

public static boolean isValidCode (String code) {
Pattern p = Pattern.compile("[fFtTvV\\-~^<>()]+"); //a-zA-Z
Matcher m = p.matcher(code);
return m.matches();
}

Related

How to match two string using java Regex

String 1= abc/{ID}/plan/{ID}/planID
String 2=abc/1234/plan/456/planID
How can I match these two strings using Java regex so that it returns true? Basically {ID} can contain anything. Java regex should match abc/{anything here}/plan/{anything here}/planID
If your "{anything here}" includes nothing, you can use .*. . matches any letter, and * means that match the string with any length with the letter before, including 0 length. So .* means that "match the string with any length, composed with any letter". If {anything here} should include at least one letter, you can use +, instead of *, which means almost the same, but should match at least one letter.
My suggestion: abc/.+/plan/.+/planID
If {ID} can contain anything I assume it can also be empty.
So this regex should work :
str.matches("^abc.*plan.*planID$");
^abc at the beginning
.* Zero or more of any Character
planID$ at the end
I am just writing a small code, just check it and start making changes as per you requirement. This is working, check for your other test cases, if there is any issue please comment that test case. Specifically I am using regex, because you want to match using java regex.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class MatchUsingRejex
{
public static void main(String args[])
{
// Create a pattern to be searched
Pattern pattern = Pattern.compile("abc/.+/plan/.+/planID");
// checking, Is pattern match or not
Matcher isMatch = pattern.matcher("abc/1234/plan/456/planID");
if (isMatch.find())
System.out.println("Yes");
else
System.out.println("No");
}
}
If line always starts with 'abc' and ends with 'planid' then following way will work:
String s1 = "abc/{ID}/plan/{ID}/planID";
String s2 = "abc/1234/plan/456/planID";
String pattern = "(?i)abc(?:/\\S+)+planID$";
boolean b1 = s1.matches(pattern);
boolean b2 = s2.matches(pattern);

Java regex (java.util.regex). Search for dollar sign

I have a search string.
When it contains a dollar symbol, I want to capture all characters thereafter, but not include the dot, or a subsequent dollar symbol.. The latter would constitute a subsequent match.
So for either of these search strings...:
"/bla/$V_N.$XYZ.bla";
"/bla/$V_N.$XYZ;
I would want to return:
V_N
XYZ
If the search string contains percent symbols, I also want to return what's between the pair of % symbols.
The following regex seems do the trick for that.
"%([^%]*?)%";
Inferring:
Start and end with a %,
Have a capture group - the ()
have a character class containing anything except a % symbol, (caret infers not a character)
repeated - but not greedily *?
Where some languages allow %1, %2, for capture groups, Java uses backslash\number syntax instead. So, this string compiles and generates output.
I suspect the dollar symbol and dot need escaping, as they are special symbols:
$ is usually end of string
. is a meta sequence for any character.
I have tried using double backslash symbols.. \
Both as character classes .e.g. [^\\.\\$%]
and using OR'd notation %|\\$
in attempts to combine this logic and can't seem to get anything to play ball.
I wonder if another pair of eyes can see how to solve this conundrum!
My attempts so far:
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
String search = "/bla/$V_N.$XYZ.bla";
String pattern = "([%\\$])([^%\\.\\$]*?)\\1?";
/* Either % or $ in first capture group ([%\\$])
* Second capture group - anything except %, dot or dollar sign
* non greedy group ( *?)
* then a backreference to an optional first capture group \\1?
* Have to use two \, since you escape \ in a Java string.
*/
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(search);
List<String> results = new ArrayList<String>();
while (m.find())
{
for (int i = 0; i<= m.groupCount(); i++) {
results.add(m.group(i));
}
}
for (String result : results) {
System.out.println(result);
}
}
}
The following links may be helpful:
An interactive Java playground where you can experiment and copy/paste code.
Regex101
Java RegexTester
Java backreferences (The optional backreference \\1 in the Regex).
Link that summarises Regex special characters often found in languages
Java Regex book EPub link
Regex Info Website
Matcher class in the Javadocs
You may use
String search = "/bla/$V_N.$XYZ.bla";
String pattern = "[%$]([^%.$]*)";
Matcher matcher = Pattern.compile(pattern).matcher(search);
while (matcher.find()){
System.out.println(matcher.group(1));
} // => V_N, XYZ
See the Java demo and the regex demo.
NOTE
You do not need an optional \1? at the end of the pattern. As it is optional, it does not restrict match context and is redundant (as the negated character class cannot already match neither $ nor%)
[%$]([^%.$]*) matches % or $, then captures into Group 1 any zero or more
chars other than %, . and $. You only need Group 1 value, hence, matcher.group(1) is used.
In a character class, neither . nor $ are special, thus, they do not need escaping in [%.$] or [%$].

How regex lookaround works when used alone

public class Test {
public static void main(String[] args){
Pattern a = Pattern.compile("(?=\\.)|(?<=\\.)");
Matcher b = a.matcher(".");
while (b.find()) System.out.print("+");
}
}
I've been reading the lookaround section on Regular-Expressions.info and trying to figure out how it works, and I'm stuck with this thing. when I run the code above the result is ++, which I don't understand, because since "." is the only token to match the pattern against, and apparently there's nothing behind or ahead of the "." so how can it match twice?
As the regex engine advances through the input, it considers both characters and positions before and after characters as distinct positions within the input.
Your input has 3 positions:
Just before the first character
The first character
Just after the first character
Position 1 matches (?=\\.).
Position 3 matches (?<=\\.).

How to use regular expression to replace non-digits and math operators together?

How do I only keep chars of [0-9] and [+-*/] in a string in Java? My approach is to use a union to create a single character class comprised of [0-9] and [+-*/] character classes, but I got an empty string.
Here is an example string I use: 10+2*2-5
public void cleanup(String s){
String regex = "[^0-9[^+-*//]]";
String tmp = s.replaceAll(regex, "");
System.out.println(tmp);
}
You want you character class ([...]) to include the range 0-9 and the additional characters *, /, - and +. All you need is to put them one after the other and escape - (\\-), unless it's the last character. Then, use a negation construct (^) inside at the beginning:
public class Example {
public static void main(String[] args) {
String test = "a3f6+[,b7*\"d/-8u";
System.out.println(test.replaceAll("[^0-9/*+-]", ""));
}
}
Outputs
36+7*/-8
What about s/[^0-9]|[^+-*//]//g for the regex?
You have an issue there with the way you have use - in the second part, if you use - in the middle of an expression like you have in [^+-*/] it thinks that is part of a range expression like you did with 0-9 so you need to put the - at the end of the expression so that it isn't treated as a range.
The following expression should do what you are after:
[^0-9*/+-]

Translate regex for Java

How can this regex ^\d+(?:[\.\,]\d+)?$ to be usable with Java.
input.matches("^\\d+(?:[\\.\\,]\\d+)?$"); // Redundant character escape
Your expression is fine. Note: Using matches; implicitly adds ^ at the start and $ at the end of your pattern. Also, you do not need to escape the characters inside of your character class.
input.matches("\\d+(?:[,.]\\d+)?");
Your code executes fine
public static void main(String[] args) throws Exception {
String input = "123";
System.out.println(input.matches("^\\d+(?:[\\.\\,]\\d+)?$"));
input = "123.123";
System.out.println(input.matches("^\\d+(?:[\\.\\,]\\d+)?$"));
input = "123,123";
System.out.println(input.matches("^\\d+(?:[\\.\\,]\\d+)?$"));
input = "123..123";
System.out.println(input.matches("^\\d+(?:[\\.\\,]\\d+)?$"));
}
prints
true
true
true
false
As per regex101, your matched string will start with one ore more digits, followed by a non capturing group that occurs once or not at all, containing a . or a ,, literally, and one or more digits, and then end.
That's what you have, that's what will match.

Categories