Regular Expression to replace a group

regex replace group c#
regex capture group
regex replace group javascript
regex optional capture group
regex backreference
python regex replace group
regex named capture group
regex non capturing group

I want to replace the space before the digits with some characters but i couldn't do that with the following regex:

    String parentString = " 1.skdhhfsdl 2. hkjkj 3.234hbn,m";
    String myregex = "/(\\s)[1-9]+./";
    String output = parentString.replaceAll(myregex, "$1ppp");
    System.out.println(output);

Please help me solve the regex.

UPDATE

After implementing the suggestion by @CertainPerformant and @Wiktor my code looks like,

String myregex = "(\\s)[1-9]+.";
String output = parentString.replaceAll(myregex, "\n");

I want the output to be like

1.skdhhfsdl
2. hkjkj
3.234hbn,m

But, i am currently getting

1.skdhhfsdl
 hkjkj
234hbn,m

You should use the regex

\s+([1-9]+\.)

Notice that I captured the number part instead. When you are replacing, you usually capture the part you want to keep. Also note that I removed the leading and trailing slashes as those are not needed in Java. The . should also be escaped, like I did here.

The replacement is \n$1, meaning "new line, then group 1".

String parentString = " 1.skdhhfsdl 2. hkjkj 3.234hbn,m";
String myregex = "\\s+([1-9]+\\.)";
String output = parentString.replaceAll(myregex, "\n$1");
System.out.println(output);

Find and replace text using regular expressions, Use regex capturing groups and backreferences. You can put the regular expressions inside brackets in order to group them. Each group has a number starting  Let's suppose I have the following regex: and I want to replace, using C#, the Group 1 (\d+) with AA, to obtain: Now I'm replacing it using: But I don't really like this, because if I change the pattern to match _ (\d+)_ instead, I will have to change the replacement string by _AA_ too, and this is against the DRY principle.

How about:

String myregex = "\\s([1-9]+\\.)";
String output = parentString.replaceAll(myregex, "\n$1");

Substitutions in Regular Expressions, This is the first capturing group. Because the replacement pattern is $1 , the call to the Regex.Replace method replaces the entire matched  If name doesn't specify a valid named capturing group defined in the regular expression pattern but consists of digits, ${name} is interpreted as a numbered group. If name specifies neither a valid named capturing group nor a valid numbered capturing group defined in the regular expression pattern, ${ name } is interpreted as a literal character sequence that is used to replace each match.

In order to get the expected output, you can use this regex.

public static void main(String[] args) throws Exception {
    String parentString = " 1.skdhhfsdl 2. hkjkj 3.234hbn,m";
    String myregex = "\\s+(?=[1-9]+\\.)";
    String output = parentString.trim().replaceAll(myregex, "\n");
    System.out.println(output);
}

This only matches one or more space that are followed by a number and dot and only replaces the space (because of lookahead) with a new line. parentString.trim() ensures that you don't get a newline before your first line.

Output:

1.skdhhfsdl
2. hkjkj
3.234hbn,m

How to replace captured groups only?, Let's make something more complex – a regular expression to search We can fix it by replacing \w with [\w-] in every word except the last one:  Numbered Backreferences. If your regular expression has named or numbered capturing groups, then you can reinsert the text matched by any of those capturing groups in the replacement text. Your replacement text can reference as many groups as you like, and can even reference the same group more than once.

Capturing groups, Regular Expression Capture Groups. Replacement Syntax for Group 10 To insert the capture in the replacement string, you must use the group's number,  Two groups are specified in the regular expression (re); therefore, the replacement string can legally refer to up to two groups. In this case, the order of the two words is switched, while the word "bites" is preserved as is. We could even have made "bites" a group, although nothing would be gained in this case.

Regex Capture Groups and Back-References, How to specify the regex replacement for a string depending on its value. You can insert new text, you can insert text matched by capture groups, but there is  The only thing that varies from one implementation to the next is the regular expression to use, and the logic for converting each match into its replacement. 4.1. Use a Function and Pattern Input. We can use a Java Function<Matcher, String> object to allow the caller to provide the logic to process each match.

Conditional Replacement—Regex Trick, The second is the pattern to match, which looks for four groups of four numeric digits. The third provides the replacement string. In this case, the  The relevant point for using regex+capturing groups in Atom is knowing how to refer to the captured group in the Replace field. So let’s use an example: the pattern below matches and captures a 2-character-word followed by a 3-character-word.

Comments
  • Your $1 will insert the captured group. If you don't want to include the space in the replacement, remove the $1?
  • Remove the regex delimiters, it is Java. Also, if there is 1 or more spaces, use \s+. We only capture what we need to keep. When you use (\s) and $1 you keep the whitespace. Please provide some more details on what exactly your scenario looks like. Do you have control over the regex pattern? Well, try parentString.replaceAll("\\s+(?=[0-9])", "") if you need to remove those spaces before any digit.
  • thank u for your time, now i have updated the question.
  • Try parentString.replaceAll("\\s+(?=[0-9])", "\n").trim()
  • Java is not JavaScript, here regex isn't in form /regex/flags but simply regex (you can add flags with (?..) notation). Also your question and example doesn't match, since you are using match from group 1 (which is space) as part of replacement, so it will be replacing only matched number (and one additional character matched by dot . - since you didn't escape it).
  • @Samim yes it is
  • sorry its woking. tried too many suggestions and got my code messy
  • It's working for me. Ok putting everything in main method so u can run as it is.
  • Well, I am getting the output I included in my answer. What is not working and what output you are getting?