returning online numeric and the + sign - java

I have the following string : +996a34fdsc112
I want to return only the numbers with the plus sign : +99634112
How to do it using the replaceAll regex function ?

You can use:
String repl = "+996a34fdsc112".replaceAll("[^+\\d]+", "");
//=> +99634112

It seems that you want to remove all characters in range a-z. If that is the case then you can use
String noAlpha = "+996a34fdsc112".replaceAll("[a-z]+","");
+ in this case represents {1,} which means "at least once", it doesn't represent + character from your text.

Related

Replacing certain combination of characters

I'm trying to remove the first bad characters (CAP letter + dot + Space) of this.
A. Shipping Length of Unit
C. OVERALL HEIGHT
Overall Weigth
X. Max Cutting Height
I tried something like that, but it doesn't work:
string.replaceAll("[A-Z]+". ", "");
The result should look like this:
Shipping Length of Unit
OVERALL HEIGHT
Overall Weigth
Max Cutting Height
This should work:
string.replaceAll("^[A-Z]\\. ", "")
Examples
"A. Shipping Length of Unit".replaceAll("^[A-Z]\\. ", "")
// => "Shipping Length of Unit"
"Overall Weigth".replaceAll("^[A-Z]\\. ", "")
// => "Overall Weigth"
input.replaceAll("[A-Z]\\.\\s", "");
[A-Z] matches an upper case character from A to Z
\. matches the dot character
\s matches any white space character
However, this will replace every character sequence that matches the pattern.
For matching a sequence at the beginning you should use
input.replaceAll("^[A-Z]\\.\\s", "");
Without looking your code it is hard to tell the problem. but from my experience this is the common problem which generally we make in our initial days:
String string = "A. Test String";
string.replaceAll("^[A-Z]\\. ", "");
System.out.println(string);
String is an immutable class in Java. what it means once you have create a object it can not be changed. so here when we do replaceAll in existing String it simply create a new String Object. that you need to assign to a new variable or overwrite existing value something like below :
String string = "A. Test String";
string = string.replaceAll("^[A-Z]\\. ", "");
System.out.println(string);
Try this :
myString.replaceAll("([A-Z]\\.\\s)","")
[A-Z] : match a single character in the range between A and Z.
\. : match the dot character.
\s : match the space character.

Validate string a+b

I would like to validate if the particular string is true or not in form of a + b
If input = a + b true
If input = a + false
if input = + b false
where a and b can be any string characters
I can think of a couple of ways:
Use a regex to match a "+" the characters before and after it.
Use String.indexOf("+") to find a "+" character and test the value of the index to see if it as the start or end of the string.
(Don't forget the cases where a or b could contain a "+" character; i.e. multiple "+" characters in the string.)
You can use regular expression (regex) to test the string. In java you can use the Pattern and Matcher classes to test if a string matches a given regex. The regex you want to use is:
String regex = ".* \\+ .*";
This regex will test for a string in the following form: "[characters] + [characters]".
Here is more information about the regex in java.

replace a String (in some cases) in java

if i have for example String a = "8sin(30)+sin(40) + 3sin(30)"
String b = a;how I can replace only the first "sin" and the third for "*sin", mantaining the second "sin" the same?
in other word, how I can replace part of a string only in specific cases?
If you want to replace sin which have number before it you can use something like
yourString = yourString.replaceAll("(\\d+)sin","$1*sin");
replaceAll uses regular expression which represents
\\d+ string build from one or more digit characters (like 0, 12, 321...) - we will place this number in group 1
sin literal.
In replacement we are reusing match from group 1 via $1
Demo:
String a = "8sin(30)+sin(40) + 3sin(30)";
System.out.println(a.replaceAll("(\\d)+sin","$1*sin"));
Output: 8*sin(30)+sin(40) + 3*sin(30)
You can also use look-behind to check if before sin there is any digit
String a = "8sin(30)+sin(40) + 3sin(30)";
System.out.println(a.replaceAll("(?<=\\d)sin","*sin"));
You are probably looking for the replaceFirst(String regex, String replacement) method of String.
You can use below regex:
[0-9]+sin
Demo
b=b.replaceFirst("[0-9]+sin","*sin");
Also you can use the substring(int beginIndex) method.

How to replace last letter to another letter in java using regular expression

i have seen to replace "," to "." by using ".$"|",$", but this logic is not working with alphabets.
i need to replace last letter of a word to another letter for all word in string containing EXAMPLE_TEST using java
this is my code
Pattern replace = Pattern.compile("n$");//here got the real problem
matcher2 = replace.matcher(EXAMPLE_TEST);
EXAMPLE_TEST=matcher2.replaceAll("k");
i also tried "//n$" ,"\n$" etc
Please help me to get the solution
input text=>njan ayman
output text=> njak aymak
Instead of the end of string $ anchor, use a word boundary \b
String s = "njan ayman";
s = s.replaceAll("n\\b", "k");
System.out.println(s); //=> "njak aymak"
You can use lookahead and group matching:
String EXAMPLE_TEST = "njan ayman";
s = EXAMPLE_TEST.replaceAll("(n)(?=\\s|$)", "k");
System.out.println("s = " + s); // prints: s = njak aymak
Explanation:
(n) - the matched word character
(?=\\s|$) - which is followed by a space or at the end of the line (lookahead)
The above is only an example! if you want to switch every comma with a period the middle line should be changed to:
s = s.replaceAll("(,)(?=\\s|$)", "\\.");
Here's how I would set it up:
(?=.\b)\w
Which in Java would need to be escaped as following:
(?=.\\b)\\w
It translates to something like "a character (\w) after (?=) any single character (.) at the end of a word (\b)".
String s = "njan ayman aowkdwo wdonwan. wadawd,.. wadwdawd;";
s = s.replaceAll("(?=.\\b)\\w", "");
System.out.println(s); //nja ayma aowkdw wdonwa. wadaw,.. wadwdaw;
This removes the last character of all words, but leaves following non-alphanumeric characters. You can specify only specific characters to remove/replace by changing the . to something else.
However, the other answers are perfectly good and might achieve exactly what you are looking for.
if (word.endsWith("char oldletter")) {
name = name.substring(0, name.length() - 1 "char newletter");
}

Parse and remove special characters in java regex

So we were looking at some of the other regex posts and we are having trouble removing a special case in one instance; the special character is in the beginning of the word.
We have the following line in our code:
String k = s.replaceAll("([a-z]+)[()?:!.,;]*", "$1");
where s is a singular word. For example, when parsing the sentence "(hi hi hi)" by tokenizing it, and then performing the replaceAll function on each token, we get an output of:
(hi
hi
hi
What are we missing in our regex?
You can use an easier approach - replace the characters that you do not want with spaces:
String k = s.replaceAll("[()?:!.,;]+", " ");
Position matters so you would need to match the excluded charcters before the capturing group also:
String k = s.replaceAll("[()?:!.,;]*([a-z]+)[()?:!.,;]*", "$1");
your replace just removed the "special chars" after the [a-z]+, that's why the ( before hi is left there.
If you know s is a single word
you could either:
String k = s.replaceAll("\\W*(\\w+)\\W*", "$1");
or
String k = s.replaceAll("\\W*", "");
This can be more simple
try this :
String oldString = "Hi There ##$ What is %#your name?##$##$ 0123$$";
System.out.println(oldString.replaceAll("[\\p{Punct}\\s\\d]+", " ");
output :
Hi There What is your name 0123
So it also accepts numeric.
.replaceAll("[\p{Punct}\s\d]+", " ");
will replace alll the Punctuations used which includes almost all the special characters.

Categories