replace only some characters in a string - java

I am looking for a way in Java to replace a matched character from a sequence without a loop.
Example
String x = ""
String pattern = "12"
String ex1 = "1254"
x = ex1.replace(pattern, "");
System.out.print(x)
Output:
54
In this case 1254 a match is found: 12
However,
String x = ""
String pattern = "12"
String ex1 = "154"
x = ex1.replace(pattern, "");
System.out.print(x)
Output:
154
In this case no replacement takes place.
The desired output in this case would be:
54
because only 1 is found from the pattern
This is because the pattern should fully match in the word. However, is there a function where only the matched characters from the pattern will be replaced?

How about using a character class
x = ex1.replaceAll("[12]", "");

Related

How to remove alphabetic values from String and how to get desired String like in description?

I have the following string:
String textString = "Value for CH2 234 45 HH 2546";
I need a regex to remove white spaces between numbers but also want to avoid if there is a non-numeric value before a number like
String texString="23445 2546";
Here want to avoid the numerical value after any non-numeric value Here in my String want to avoid the numeric value also after H there are 2 but I just want to make a string like "23445 2546". if there is any alphabetic character is there it should be removed and add the whitespace between numbers.
static String getNumber(String textString) {
StringBuilder sb = new StringBuilder();
Matcher matcher = Pattern.compile("(?<!\\w)\\d+(?!\\w)").matcher(textString);
while (matcher.find()) {
sb.append(matcher.group());
}
return sb.toString();
}
From Above regex and code i am getting output:
String texString="234452546";
Required output:
String texString="23445 2546";
Update:
If there is String like
String textString = "Value for CH2 234 45 HH 25.46";
output:
String texString="23445 2546";
Required output:
String texString="23445 25.46";
enter code here
You may use this regex for your matches:
\b\d[\d.]*\b(?:\h(?!\h*\d))?
RegEx Demo
RegEx Explanation:
\b: Word boundary
\d: Match 1 digit
[\d.]*: Match 0 or more digits or dot
\b: Word boundary
(?:: Start optional non-capture group
\h: Match a horizontal whitespace
(?!\h*\d): Negative lookahead to assert that we don't have a digit ahead after matching 0 or more whitespaces
)?: End optional non-capture group
Code:
String textString = "Value for CH2 234 45 HH 25.46";
final String re = "\\b\\d[\\d.]*\\b(?:\\h(?!\\h*\\d))?";
StringBuilder sb = new StringBuilder();
Matcher matcher = Pattern.compile(re).matcher(textString);
while (matcher.find()) {
sb.append(matcher.group());
}
System.out.println(sb.toString());
Output:
23445 25.46
Java code using Java 9+ .results() method:
final String re = "\\b\\d[\\d.]*\\b(?:\\h(?!\\h*\\d))?";
String out = Pattern.compile(re)
.matcher(textString)
.results()
.map(MatchResult::group)
.collect(Collectors.joining());
System.out.println(out);

Extract a particular number from a string using regex in java

Here is my string
INPUT:
22 TIRES (2 defs)
1 AP(PEAR + ANC)E (CAN anag)
6 CHIC ("SHEIK" hom)
EXPECTED OUTPUT:
22 TIRES
1 APPEARANCE
6 CHIC
ACTUAL OUTPUT :
TIRES
APPEARANCE
CHIC
I tried using below code and got the above output.
String firstnames =a.split(" \\(.*")[0].replace("(", "").replace(")", "").replace(" + ",
"");
Any idea of how to extract along with the numbers ? I don't want the numbers which are after the parentheses like in the input " 22 TIRES (2 defs)". I need the output as "22 TIRES" Any help would be great !!
I am doing it bit differently
String line = "22 TIRES (2 defs)\n\n1 AP(PEAR + ANC)E (CAN anag)\n\n6 CHIC (\"SHEIK\" hom)";
String pattern = "(\\d+\\s+)(.*)\\(";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);
while (m.find()) {
String tmp = m.group(1) + m.group(2).replaceAll("[^\\w]", "");
System.out.println(tmp);
}
Ideone Demo
I would use a single replaceAll function.
str.replaceAll("\\s+\\(.*|\\s*\\+\\s*|[()]", "");
DEMO
\\s+\\(.*, this matches a space and and the following ( characters plus all the remaining characters which follows this pattern. So (CAN anag) part in your example got matched.
\\s*\\+\\s* matches + along with the preceding and following spaces.
[()] matches opening or closing brackets.
Atlast all the matched chars are replaced by empty string.

Extracting two substrings from one string

I am trying to extract the 00 and 02 from the line below into Strings.
invokestatic:indexbyte1=00 indexbyte2=02
I am using this code, but it's not working correctly:
String parse = "invokestatic:indexbyte1=00 indexbyte2=02";
String first = parse.substring(check.indexOf("=") + 1);
String second= parse.substring(check.lastIndexOf("=") + 1);
This seems to work for the seconds string, but the first strings value is
00 indexbyte2=02
I want to catch just the two digits and not the rest of the string.
If you don't specify the second parameter in substring method it will result in a substring from the starting index to the end of string that's why you get "00 indexbyte2=02" for first.
Specify the last index only to extract two digits when you extract value for first
String first = parse.substring(check.indexOf("=") + 1, check.indexOf("=") + 3);
You can use a regex pattern with groups, like this:
public static void main(String[] args) {
String input = "invokestatic:indexbyte1=00 indexbyte2=02";
Pattern pattern = Pattern.compile(".*indexbyte1=(\\d*) indexbyte2=(\\d*)");
Matcher m = pattern.matcher(input);
if (m.matches()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
Try this:
String first = parse.substring(check.indexOf("=") + 1, check.indexOf("=") + 3);
check.indexOf("=") + 3 will take the 02 and will be the endindex for the substring. Presently you are not specifying the endindex hence it is taking the indexbyte2=02 as well since substring does not know where to stop hence it parses down till the end.
String parse = "invokestatic:indexbyte1=00 indexbyte2=02";
String first = parse.substring(parse.indexOf("=") + 1,
parse.indexOf("=") + 3);
String second = parse.substring(parse.lastIndexOf("=") + 1);
System.out.println(first + ", " + second);
You could use Pattern, Matcher clases.
Matcher m = Pattern.compile("(?<==)\\d+").matcher(string);
while(m.find())
{
System.out.println(m.group());
}
substring also has an endIndex. See the docs: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)
If the input has the basic form invokestatic:indexbyte1=00 indexbyte2=02 ... indexbyte99=99 you could use a regex:
Pattern p = Pattern.compile("indexbyte\\d+=([a-fA-F0-9]{2})");
Matcher m = p.matcher(input);
while( m.find() ) {
String idxByte = m.group(1);
//handle the byte here
}
This assumes that the identifier for those bytes is indexbyteN but this can be replaced with another identifier. Further this assumes the bytes are provided in hex, i.e. 2 hex characters (case insensitive here).

How to find a string including non word character?

How to find a string including non word character?
Example input: l l'oreal s.a. l l' ab l
search String: l
output: XX l'oreal s.a. XX l' ab l
Expected: XX l'oreal s.a. XX l' ab X
I was trying to find the search string in the input string using the below regex.
String inputStr = "l l'oreal s.a. l l' ab l";
System.out.println(inputStr);
String searchStr = "l";
Pattern pattern = Pattern.compile("(\\b"+ searchStr +"\\b)(?=[\\s])");
Matcher matcher = pattern.matcher(inputStr);
if ( matcher.find()){
String rep = matcher.replaceAll("XX");
System.out.println("REP:" + rep);
}else{
System.out.println("no match...");
}
The regex pattern searches for the string where it is followed by a space(\s). But in the above example it doesn't work for the last character since it was not followed by a space.
The main goal was to find string with non word characters like...
private-limited ( when searching for private should return false)
Hello! ( false when searched for Hello)
Couple of patterns which tried but not working...
pattern = Pattern.compile("(?<![\\w+])" + searchStr + "(?![\\W+])", Pattern.CASE_INSENSITIVE);
pattern = Pattern.compile("(?<=[\\s])(\\b"+ searchStr +"\\b)(?=\\s)");
In the above example if I replace searchString = "l'" it doesn't match anything.
Is my approach correct?
What is that I am missing?
Thanks.
An easy solution, if you can modify the string to search, would be to concatenate it with a space before and afterwards, then simply search for " l "
Perhaps word boundary followed by whitespace or end of input.
In your case for l
\b(l)(\s+|$)
Demonstration

Why Matcher fails for Strings obtained or provide at Runtime in Java?

Hi I was recently developing a code where i had to extract the last 3 group of digits. So i used pattern to extract the data. But i failed to understand. CAN any one help me to understand it ??
String str ="EGLA 0F 020";
String def = "ALT 1F 001 TO ALT 1F 029";
String arr[] = def.split("TO");
String str2 = arr[0];
System.out.println("str2:"+str2);
Pattern pt = Pattern.compile("[0-9][0-9][0-9]$");
Matcher m1 = pt.matcher(str);
Matcher m2 = pt.matcher(str2);
boolean flag = m1.find();
boolean flag2 = m2.find();
if(flag)
System.out.println("first match:::"+m1.group(0));
else
System.out.println("Not found");
if(flag2)
System.out.println("first match:::"+m2.group(0));
else
System.out.println("Not found");
The output produced for the above code is As follows:::
str2:ALT 1F 001
first match:::020
Not found
Please Do reply iam stuck here ??
It's because when you split you have a trailing space.
String str = "EGLA 0F 020";
String str2 = "ALT 1F 001 ";
// ^ trailing space
You could fix it a number of ways. For example:
by splitting on " TO "
trimming the result
allowing trailing spaces in your regular expression.
For example, this change would work:
String arr[] = def.split(" TO ");
If you notice your split take effect only on the letters "TO", it means str2 pattern is "ALT 1F 001 ".
To resolve this you can try to split on "\s*TO\s*" instead of "TO" so that any spaces surrounding the work TO would be removed too. Another solution would be to replace your pattern "[0-9][0-9][0-9]$" with "[0-9][0-9][0-9]" without the final $, so that it would accept ending spaces on your String.
Try this pattern:
Pattern pattern = Pattern.compile("[0-9][0-9][0-9]\\s*$");
or
Pattern pattern = Pattern.compile("[0-9]{3}\\s*$");

Categories

Resources