Replacing substrings in a string using a regular expression group


username

I can't find a proper way to remove substrings that are case-insensitive equal to "null" and replace a huge input data string (with many lines and uses) with an empty string ; as a delimiter.

To simplify, here is an example of what I'm looking for:

input string

Steve;nuLL;2;null\n
null;nullo;nUll;Marc\n
....

expected output

Steve;;2;\n
;nullo;;Marc\n
...

code

Matcher matcher = Pattern.compile("(?i)(^|;)(null)(;|$)").matcher(dataStr);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
    matcher.appendReplacement(sb, matcher.group(1) + "" + matcher.group(3));
}
return sb.toString();

Can it be solved using regular expressions?

edit:

From the Java code above, I only get the first match ever replaced, but not every appearance in the row and data flow. For whatever reason, the matcher.find()action is only performed once.

Joop Eggen
return dataStr.replaceAll("(?smi)\\bnull\\b", "");
  • \bis the word boundary.
  • (?i)is an i=case-ignoring command.
  • ( (?s)It's DOT_ALL, which also .matches newlines.)
  • (?m)is MULTI_LINE.

You forgot , all after the appendTaillast replacement . If the string contains multiple lines, add the MULTI_LINE option to reinterpret the ^sum $. See the javadoc Pattern.

while (matcher.find()) {
    matcher.appendReplacement(sb, matcher.group(1) + "" + matcher.group(3));
}
matcher.appendTail(sb);

Or use lambda:

String result = matcher.replaceAll(mr -> mr.group(1) + mr.group(3));

Among them mris the free naming MatchResultoffer replaceAll.

Related


Replacing combination of characters using regular expression Java

Wandering monk: I have several datasets of strings in which I want to replace certain character combinations using regex replacement. I've tried multiple modes and none of them helped. Can someone point me to the correct pattern? E.g: hello, [cicuyuv v,] imijm

Replacing substrings in entire long string using StringBuffer

comet: I need to write a long string (4324 characters) and use StringBuffer for this purpose. Most of the string will be spaces, but some parts will contain orderdetails. They have to appear somewhere. This string will be entered into the billing system. I'm i

Regular expression to search for substrings

Bao Zan Suppose I have a file likeMichael is studying at the Faculty of Economics at the University and I need to check if a given string contains the following expression:Facul* of Econom* An asterisk indicates that the word can have many different endings Us

Regular expression for selecting substrings

Wabaf I'm trying to write a regular expression that will select certain parts of a string and ignore the rest I have a text below and want the regex to extract the string """ from all lines the text to select """ (ignoring spaces) and ignore the rest of the st

Replacing substrings in a string using a regular expression group

username I can't find a proper way to remove substrings that are case-insensitive equal to "null" and replace a huge input data string (with many lines and uses) with an empty string ; as a delimiter. To simplify, here is an example of what I'm looking for: in

Extract two substrings using regular expression matching

Terry I have a simple regex pattern and a string. The regex pattern is (\d*)\*(C.*), the string is 32*C1234*3. I want to extract the value 32and C1234*3pattern from the string using regex. I can achieve that in Perl using the following code: my $x = "32*C1234*

Regular expression to match substrings in a string

Kimmamir I want to capture strings using this regex: ^([\d]_[a-z|A-Z]+_test)$ So it will capture something like:1_mytest_test However, if the string is inside another string like: results.1_mytest_test.=fail, more info There are no matches. I thought the ^su

Replacing substrings in entire long string using StringBuffer

comet: I need to write a long string (4324 characters) and use StringBuffer for this purpose. Most of the string will be spaces, but some parts will contain orderdetails. They have to appear somewhere. This string will be entered into the billing system. I'm i

Replacing substrings in entire long string using StringBuffer

comet: I need to write a long string (4324 characters) and use StringBuffer for this purpose. Most of the string will be spaces, but some parts will contain orderdetails. They have to appear somewhere. This string will be entered into the billing system. I'm i

Replacing combination of characters using regular expression Java

Wandering monk: I have several datasets of strings in which I want to replace certain character combinations using regex replacement. I've tried multiple modes and none of them helped. Can someone point me to the correct pattern? E.g: hello, [cicuyuv v,] imijm

Replacing combination of characters using regular expression Java

Wandering monk: I have several datasets of strings in which I want to replace certain character combinations using regex replacement. I've tried multiple modes and none of them helped. Can someone point me to the correct pattern? E.g: hello, [cicuyuv v,] imijm

Match substrings using regular expression patterns

Anita I am trying to match substrings using regular expressions. An example of the substring I get is: "{{simple-array.text}}{{simple-array.option}}". Using the following pattern: {(.*?)}, I get the following two matches: {{simple-array.text}and {{simple-array

Replacing substrings in a string using a list

Jadi I'm looking for a more pythonic way to replace placeholders of known length in a string with values from a list. They should be replaced in sequence and should only be used once. For example, use the following values: replaceVals = ['foo', 'bar'] origStr

Replacing substrings in a string using a regular expression group

username I can't find a proper way to remove substrings that are case-insensitive equal to "null" and replace a huge input data string (with many lines and uses) with an empty string ; as a delimiter. To simplify, here is an example of what I'm looking for: in

Match substrings using regular expression patterns

Anita I am trying to match substrings using regular expressions. An example of the substring I get is: "{{simple-array.text}}{{simple-array.option}}". Using the following pattern: {(.*?)}, I get the following two matches: {{simple-array.text}and {{simple-array

Replacing nth character using regular expression in Java

raymx007 I am trying to learn regular expressions in Java. So far I've been trying some small challenges and I'm wondering if there is a way to define the nth character. For example, let's say I have this string: todayiwasnotagoodday If I want to replace the t

Replacing substrings in entire long string using StringBuffer

comet: I need to write a long string (4324 characters) and use StringBuffer for this purpose. Most of the string will be spaces, but some parts will contain orderdetails. They have to appear somewhere. This string will be entered into the billing system. I'm i

Replacing combination of characters using regular expression Java

Wandering monk: I have several datasets of strings in which I want to replace certain character combinations using regex replacement. I've tried multiple modes and none of them helped. Can someone point me to the correct pattern? E.g: hello, [cicuyuv v,] imijm

C: Replacing substrings in a string using a loop

Benjamin Song I'm struggling with the concept of replacing substrings in a string. This particular exercise does not expect you to use <string.h>the built-in functions in or <strings.h>. Given a string consisting of the following two lines: "Mr. Fay, is this g

Replacing substrings in entire long string using StringBuffer

comet: I need to write a long string (4324 characters) and use StringBuffer for this purpose. Most of the string will be spaces, but some parts will contain orderdetails. They have to appear somewhere. This string will be entered into the billing system. I'm i

Match substrings using regular expression patterns

Anita I am trying to match substrings using regular expressions. An example of the substring I get is: "{{simple-array.text}}{{simple-array.option}}". Using the following pattern: {(.*?)}, I get the following two matches: {{simple-array.text}and {{simple-array

Replacing substrings in a string using a regular expression group

username I can't find a proper way to remove substrings that are case-insensitive equal to "null" and replace a huge input data string (with many lines and uses) with an empty string ; as a delimiter. To simplify, here is an example of what I'm looking for: in

Extract two substrings using regular expression matching

Terry I have a simple regex pattern and a string. The regex pattern is (\d*)\*(C.*), the string is 32*C1234*3. I want to extract the value 32and C1234*3pattern from the string using regex. I can achieve that in Perl using the following code: my $x = "32*C1234*

Replacing substrings in a string using a list

Jadi I'm looking for a more pythonic way to replace placeholders of known length in a string with values from a list. They should be replaced in sequence and should only be used once. For example, use the following values: replaceVals = ['foo', 'bar'] origStr

Match substrings using regular expression patterns

Anita I am trying to match substrings using regular expressions. An example of the substring I get is: "{{simple-array.text}}{{simple-array.option}}". Using the following pattern: {(.*?)}, I get the following two matches: {{simple-array.text}and {{simple-array