I am trying to implement substring replacement, but I am not getting the desired results. Can someone comment on what I may missing here?
public class SubtringReplacement {
public static void main (String[] args){
String input = "xPIy";
if (input.contains("PI") || input.contains("pi") || input.contains("Pi")){
input.replace("PI", "3.14");
}
System.out.println(input);
}
}
Strings are immutable!!
input = input.replace("PI", "3.14");
One problem is that you need to capture the return value when you do the replacement. The other problem is that you will only replace upper case "PI", while it seems you want to replace mixed case instances. Try this instead:
input = input.replaceAll("(PI|pi|Pi)", "3.14");
replace looks for a literal match; replaceAll does a regular expression match, which is what you need.
By the way, you don't need the if condition -- if there is no match, there will be no replacement.
P.S. Look at the comment by #NullUserException if you also want to replace instances of "pI".
Obviously you were missing the Left hand assignment to make your code working with given conditions.
input.replace("PI", "3.14");
But it will only replace, if input contains PI while it will attempt for pi and Pi as well. To better handle this, I think you can use "[pP][iI]" or "[pP]{1}[iI]{1}" as replacement pattern, which will look for one occurrence of P or p followed by one occurrence of I or i e.g. below:
String input = "xPIyPizpIapib";
input = input.replaceAll("[pP][iI]", "3.14");
System.out.println(input); //<- all "pi"s irrespective of case are replaced.
String input = "xPIyPizpIapib";
input = input.replaceAll("[pP]{1}[iI]{1}", "3.14");
System.out.println(input); //<- all "pi"s irrespective of case are replaced.
Please Note: This will also replace pI as wel, if found.
Related
I am currently doing an assignment for class and I would like to know how to find an integer inside a string input. So far in the code I have created a way to exit the loop. Please don't give me code just give me some ideas. Keep in mind that I am quite new to java so please bear with me. Thanks.
EDIT: I forgot to mention that the string input should be like "woah123" and it should find only the "123" portion. Sorry
import java.util.Scanner;
public class DoubleTheInt
{
public static void main(String[] args)
{
int EXIT = 0;
while(EXIT == 0)
{
Scanner kbReader = new Scanner(System.in);
System.out.println("What is your sentence?");
String sentence = kbReader.next();
if(sentence.equalsIgnoreCase("exit"))
{
break;
}
}
}
}
For learning purpose what you can do is traverse the whole string and check only for the digits. In this case you will also learn how to check char-by-char in a string if in future you may require this also you will get the digits of that string. Hope that solves your problem.
Here's what you do...
Replace all non numeric characters with empty string using \\D and String.replaceAll function
Parse your string (after replacing) as integer using Integer.parseInt()
Edited after Christian's comment :
replaceAll() function replaces occurances of particular String (Regex is first argument) with that of the second argument String..
\\D is used to select everything except the numbers in the String. So, the above 2 lines combined will give "1234" if your String is "asas1234" .
Now , Integer.parseInt is used to convert a String to integer.. It takes a String as argument and returns an Integer.
Since you are not asking for code , I am giving you some suggestions.
Use regex in string method to find the numbers and to remove all the
non numbers.
Parse the string to integer.
Unless your assignment "is" a regex assignment, I suggest you do it the non-regex way. i.e, by reading character by character and checking for integers or reading the string and converting to a char array and process.
I'm not sure what your teacher intends you to do, but there are two ways-
Read character by character and filter the numbers by their ASCII code. Use BuffferedReader to read from standard input. And use read() method. Figure out the ASCII code range for numbers by experimenting.
Read the entire String at once(using either Scanner or BufferedReader) and see what you can do from the String API(as in the methods available for String).
Use Regular Expression : \d+
String value = "abc123";
Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(value);
int i = Integer.valueOf(m.group(1));
System.out.println(i);
output
123
I want to remove parenthesis using Java regular expression but I faced to error No group 1 please see my code and help me.
public String find_parenthesis(String Expr){
String s;
String ss;
Pattern p = Pattern.compile("\\(.+?\\)");
Matcher m = p.matcher(Expr);
if(m.find()){
s = m.group(1);
ss = "("+s+")";
Expr = Expr.replaceAll(ss, s);
return find_parenthesis(Expr);
}
else
return Expr;
}
and it is my main:
public static void main(String args[]){
Calculator c1 = new Calculator();
String s = "(4+5)+6";
System.out.println(s);
s = c1.find_parenthesis(s);
System.out.println(s);
}
The simplest method is to just remove all parentheses from the string, regardless of whether they are balanced or not.
String replaced = "(4+5)+6".replaceAll("[()]", "");
Correctly handling the balancing requires parsing (or truly ugly REs that only match to a limited depth, or “cleverness” with repeated regular expression substitutions). For most cases, such complexity is overkill; the simplest thing that could possibly work is good enough.
What you want is this: s = s.replaceAll("[()]","");
For more on regex, visit regex tutorial.
You're getting the error because your regex doesn't have any groups, but I suggest you use this much simpler, one-line approach:
expr = expr.replaceAll("\\((.+?)\\)", "$1");
You can't do this with a regex at all. It won't remove the matching parentheses, just the first left and the first right, and then you won't be able to get the correct result from the expression. You need a parser for expressions. Have a look around for recursive descent ezpresssion parsers, the Dijkstra shunting-yard algorithm, etc.
The regular expression defines a character class consisting of any whitespace character (\s, which is escaped as \s because we're passing in a String), a dash (escaped because a dash means something special in the context of character classes), and parentheses. Try it working code.
phoneNumber.replaceAll("[\\s\\-()]", "");
I know I'm very late here. But, just in case you're still looking for a better answer. If you want to remove both open and close parenthesis from a string, you can use a very simple method like this:
String s = "(4+5)+6";
s=s.replaceAll("\\(", "").replaceAll("\\)","");
If you are using this:
s=s.replaceAll("()", "");
you are instructing the code to look for () which is not present in your string. Instead you should try to remove the parenthesis separately.
To explain in detail, consider the below code:
String s = "(4+5)+6";
String s1=s.replaceAll("\\(", "").replaceAll("\\)","");
System.out.println(s1);
String s2 = s.replaceAll("()", "");
System.out.println(s2);
The output for this code will be:
4+5+6
(4+5)+6
Also, use replaceAll only if you are in need of a regex. In other cases, replace works just fine. See below:
String s = "(4+5)+6";
String s1=s.replace("(", "").replace(")","");
Output:
4+5+6
Hope this helps!
In Java for String class there is a method called matches, how to use this method to check if my string is having only digits using regular expression. I tried with below examples, but both of them returned me false as result.
String regex = "[0-9]";
String data = "23343453";
System.out.println(data.matches(regex));
String regex = "^[0-9]";
String data = "23343453";
System.out.println(data.matches(regex));
Try
String regex = "[0-9]+";
or
String regex = "\\d+";
As per Java regular expressions, the + means "one or more times" and \d means "a digit".
Note: the "double backslash" is an escape sequence to get a single backslash - therefore, \\d in a java String gives you the actual result: \d
References:
Java Regular Expressions
Java Character Escape Sequences
Edit: due to some confusion in other answers, I am writing a test case and will explain some more things in detail.
Firstly, if you are in doubt about the correctness of this solution (or others), please run this test case:
String regex = "\\d+";
// positive test cases, should all be "true"
System.out.println("1".matches(regex));
System.out.println("12345".matches(regex));
System.out.println("123456789".matches(regex));
// negative test cases, should all be "false"
System.out.println("".matches(regex));
System.out.println("foo".matches(regex));
System.out.println("aa123bb".matches(regex));
Question 1:
Isn't it necessary to add ^ and $ to the regex, so it won't match "aa123bb" ?
No. In java, the matches method (which was specified in the question) matches a complete string, not fragments. In other words, it is not necessary to use ^\\d+$ (even though it is also correct). Please see the last negative test case.
Please note that if you use an online "regex checker" then this may behave differently. To match fragments of a string in Java, you can use the find method instead, described in detail here:
Difference between matches() and find() in Java Regex
Question 2:
Won't this regex also match the empty string, "" ?*
No. A regex \\d* would match the empty string, but \\d+ does not. The star * means zero or more, whereas the plus + means one or more. Please see the first negative test case.
Question 3
Isn't it faster to compile a regex Pattern?
Yes. It is indeed faster to compile a regex Pattern once, rather than on every invocation of matches, and so if performance implications are important then a Pattern can be compiled and used like this:
Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher("1").matches());
System.out.println(pattern.matcher("12345").matches());
System.out.println(pattern.matcher("123456789").matches());
You can also use NumberUtil.isNumber(String str) from Apache Commons
Using regular expressions is costly in terms of performance. Trying to parse string as a long value is inefficient and unreliable, and may be not what you need.
What I suggest is to simply check if each character is a digit, what can be efficiently done using Java 8 lambda expressions:
boolean isNumeric = someString.chars().allMatch(x -> Character.isDigit(x));
One more solution, that hasn't been posted, yet:
String regex = "\\p{Digit}+"; // uses POSIX character class
You must allow for more than a digit (the + sign) as in:
String regex = "[0-9]+";
String data = "23343453";
System.out.println(data.matches(regex));
Long.parseLong(data)
and catch exception, it handles minus sign.
Although the number of digits is limited this actually creates a variable of the data which can be used, which is, I would imagine, the most common use-case.
We can use either Pattern.compile("[0-9]+.[0-9]+") or Pattern.compile("\\d+.\\d+"). They have the same meaning.
the pattern [0-9] means digit. The same as '\d'.
'+' means it appears more times.
'.' for integer or float.
Try following code:
import java.util.regex.Pattern;
public class PatternSample {
public boolean containNumbersOnly(String source){
boolean result = false;
Pattern pattern = Pattern.compile("[0-9]+.[0-9]+"); //correct pattern for both float and integer.
pattern = Pattern.compile("\\d+.\\d+"); //correct pattern for both float and integer.
result = pattern.matcher(source).matches();
if(result){
System.out.println("\"" + source + "\"" + " is a number");
}else
System.out.println("\"" + source + "\"" + " is a String");
return result;
}
public static void main(String[] args){
PatternSample obj = new PatternSample();
obj.containNumbersOnly("123456.a");
obj.containNumbersOnly("123456 ");
obj.containNumbersOnly("123456");
obj.containNumbersOnly("0123456.0");
obj.containNumbersOnly("0123456a.0");
}
}
Output:
"123456.a" is a String
"123456 " is a String
"123456" is a number
"0123456.0" is a number
"0123456a.0" is a String
According to Oracle's Java Documentation:
private static final Pattern NUMBER_PATTERN = Pattern.compile(
"[\\x00-\\x20]*[+-]?(NaN|Infinity|((((\\p{Digit}+)(\\.)?((\\p{Digit}+)?)" +
"([eE][+-]?(\\p{Digit}+))?)|(\\.((\\p{Digit}+))([eE][+-]?(\\p{Digit}+))?)|" +
"(((0[xX](\\p{XDigit}+)(\\.)?)|(0[xX](\\p{XDigit}+)?(\\.)(\\p{XDigit}+)))" +
"[pP][+-]?(\\p{Digit}+)))[fFdD]?))[\\x00-\\x20]*");
boolean isNumber(String s){
return NUMBER_PATTERN.matcher(s).matches()
}
Refer to org.apache.commons.lang3.StringUtils
public static boolean isNumeric(CharSequence cs) {
if (cs == null || cs.length() == 0) {
return false;
} else {
int sz = cs.length();
for(int i = 0; i < sz; ++i) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}
}
In Java for String class, there is a method called matches(). With help of this method you can validate the regex expression along with your string.
String regex = "^[\\d]{4}$";
String value = "1234";
System.out.println(data.matches(value));
The Explanation for the above regex expression is:-
^ - Indicates the start of the regex expression.
[] - Inside this you have to describe your own conditions.
\\\d - Only allows digits. You can use '\\d'or 0-9 inside the bracket both are same.
{4} - This condition allows exactly 4 digits. You can change the number according to your need.
$ - Indicates the end of the regex expression.
Note: You can remove the {4} and specify + which means one or more times, or * which means zero or more times, or ? which means once or none.
For more reference please go through this website: https://www.rexegg.com/regex-quickstart.html
Offical regex way
I would use this regex for integers:
^[-1-9]\d*$
This will also work in other programming languages because it's more specific and doesn't make any assumptions about how different programming languages may interpret or handle regex.
Also works in Java
\\d+
Questions regarding ^ and $
As #vikingsteve has pointed out in java, the matches method matches a complete string, not parts of a string. In other words, it is unnecessary to use ^\d+$ (even though it is the official way of regex).
Online regex checkers are more strict and therefore they will behave differently than how Java handles regex.
Try this part of code:
void containsOnlyNumbers(String str)
{
try {
Integer num = Integer.valueOf(str);
System.out.println("is a number");
} catch (NumberFormatException e) {
// TODO: handle exception
System.out.println("is not a number");
}
}
I am trying to match input data from the user and search if there is a match of this input.
for example if the user type : A*B*C*
i want to search all word which start with A and contains B and B
i tried this code and it;s not working:(get output false)
public static void main(String[] args)
{
String envVarRegExp = "^A[^\r\n]B[^\r\n]C[^\r\n]";
Pattern pattern = Pattern.compile(envVarRegExp);
Matcher matcher = pattern.matcher("AmBmkdCkk");
System.out.println(matcher.find());
}
Thanks.
You don't really need Regex here. Simple String class methods will work: -
String str = "AfasdBasdfCa";
if (str.startsWith("A") && str.contains("B") && str.contains("C")) {
System.out.println("true");
}
Note that this will not ensure that your B and C are in specific order, which I assume you don't need as you have not mentioned anything about that.
If you want them to be in some order (like B comes before C then use this Regex: -
if (str.matches("^A.*B.*C.*$")) {
System.out.println("true");
}
Note that, . will match any character except newline. So, you can use it instead of [^\r\n], its more clear. And you need to use the quantifier * because you need to match any repetition of the characters before B or C is found.
Also, String.matches matches the complete string, and hence the anchors at the ends.
I thing you should use * modifier in your regex like this (for 0 or more matches between A & B and then between B & C):
String envVarRegExp = "^A[^\r\n]*B[^\r\n]*C";
EDIT: It appears that you're working off the input coming from your user where user can use asterisk * in inputs. If that is the case consider this:
String envVarRegExp = userInput.replace("*", ".*?");
Where userInput is String like this:
String userInput = "a*b*c*d*e";
You need to add quantifiers to your character classes;
String envVarRegExp = "^A[^\r\n]*B[^\r\n]*C[^\r\n]*$";
I'm trying to replace the last dot in a String using a regular expression.
Let's say I have the following String:
String string = "hello.world.how.are.you!";
I want to replace the last dot with an exclamation mark such that the result is:
"hello.world.how.are!you!"
I have tried various expressions using the method String.replaceAll(String, String) without any luck.
One way would be:
string = string.replaceAll("^(.*)\\.(.*)$","$1!$2");
Alternatively you can use negative lookahead as:
string = string.replaceAll("\\.(?!.*\\.)","!");
Regex in Action
Although you can use a regex, it's sometimes best to step back and just do it the old-fashioned way. I've always been of the belief that, if you can't think of a regex to do it in about two minutes, it's probably not suited to a regex solution.
No doubt get some wonderful regex answers here. Some of them may even be readable :-)
You can use lastIndexOf to get the last occurrence and substring to build a new string: This complete program shows how:
public class testprog {
public static String morph (String s) {
int pos = s.lastIndexOf(".");
if (pos >= 0)
return s.substring(0,pos) + "!" + s.substring(pos+1);
return s;
}
public static void main(String args[]) {
System.out.println (morph("hello.world.how.are.you!"));
System.out.println (morph("no dots in here"));
System.out.println (morph(". first"));
System.out.println (morph("last ."));
}
}
The output is:
hello.world.how.are!you!
no dots in here
! first
last !
The regex you need is \\.(?=[^.]*$). the ?= is a lookahead assertion
"hello.world.how.are.you!".replace("\\.(?=[^.]*$)", "!")
Try this:
string = string.replaceAll("[.]$", "");