-2

I am having hard time solving this particular problem

input: [{"acks":"6"},{"acks":"7"},{"acks":"8"}]
regex: [\{]*["acks:"]
current output: [6},7},8}]
desired output: [6,7,8]

I am facing problem with the ending curly braces.

Thank you for helping me in advance!!

Barmar
  • 741,623
  • 53
  • 500
  • 612
dumb_coder
  • 315
  • 5
  • 21
  • 2
    Your input looks like JSON. You should use a JSON parser, not a regex, to extract data from it. – Barmar Jun 29 '23 at 16:15
  • I don't see how you're getting that result with your regexp. `["acks:"]` doesn't match numbers, commas, curly braces, or square brackets. See https://regex101.com/r/oDQbM8/1 – Barmar Jun 29 '23 at 16:18
  • Please show your actual Java code. – Barmar Jun 29 '23 at 16:18
  • @Barmar there is actually no java code. I am running the regex on nifi. I am using the validator https://www.regexplanet.com/advanced/java/index.html – dumb_coder Jun 29 '23 at 16:37
  • 1
    Since you want to leave out `[`,`]`,`,` and digits, you can replace everything rlse with empty string. Try [`[^\d,[\]]+`](https://regex101.com/r/kPq2E5/2) – markalex Jun 29 '23 at 16:41
  • And if you only need to extract digits from array of "acks", you could try replacing `\{"acks":"(\d+)"\}` with `$1`. https://regex101.com/r/XYKaLJ/1 – markalex Jun 29 '23 at 16:43
  • 1
    It seems like you're confused about how `[]` works in regex. It's not for grouping substrings, it's for matching any of a set of characters. – Barmar Jun 29 '23 at 17:00
  • @dumb_coder, it would be benefitial for your question and anybody who attempts to answer it (and subsequently help you) if you'll add explanation what exactly happens, how it is used right now, and what you are trying to achieve with this regex. (not in comments, but with [edit]) – markalex Jun 29 '23 at 18:26

4 Answers4

1

You can try this regexp (?:\"[\\w]+\":\")(\\d+)

import java.util.regex.*;
import java.util.*;
import java.util.function.Supplier;

class HelloWorld {
    public static void main(String[] args) {

    Pattern p = Pattern.compile("(?:\"[\\w]+\":\")(\\d+)");
    Matcher m = p.matcher("[{\"acks\":\"6\"},{\"acks\":\"7\"},{\"acks\":\"8\"}]\t");  

    List<String> llist = new LinkedList<String>();
    while (m.find()) {    
        llist.add(m.group(1));
    }    
    if(llist.size() == 0) {    
        System.out.println("No match found.");    
    } else {
        System.out.println("Output : " + llist.toString());
    }
   }
}
Sayan
  • 11
  • 3
  • Your new regex is equivalent to `\"\\w+\":\"(\\d+)`. There is no reason to use single `\w` inside character class. And there is no reason for use of non-capturing group. – markalex Jun 29 '23 at 19:13
0

It is unclear precisely what you are trying to do.

If all you want is to leave out [,],, and digits, you can replace everything else with empty string using the following regex: [^\d,[\]]+

And if you only need to extract digits from objects with single field "acks", you could replace \{"acks":"(\d+)"\} with $1.
Demo here.

markalex
  • 8,623
  • 2
  • 7
  • 32
0

Just check for \"acks\", the : character, and the starting and ending " characters.

The check for { doesn't seem necessary.

\"acks\":\"(.+?)\"
String string = "{\"acks\":\"6\"},{\"acks\":\"7\"},{\"acks\":\"8\"}";
Pattern pattern = Pattern.compile("\\\"acks\\\":\\\"(.+?)\\\"");
Matcher matcher = pattern.matcher(string);
List<String> list = new ArrayList<>();
while (matcher.find()) list.add(matcher.group(1));

Output

[6, 7, 8]
Reilas
  • 3,297
  • 2
  • 4
  • 17
  • It is possible, that you are missing (very much buried in comments, and partially implied information) that OP doesn't use code, and most likely is limited to replaceAll operation. – markalex Jun 29 '23 at 21:22
-1

This should work for the described case:

System.out.println("[{\"acks\":\"6\"},{\"acks\":\"17\"},{\"acks\":\"888\"}]".replaceAll("(\\{\"acks\":\")*(\"})*", ""));

Result: [6,17,888]

I can see that there are some comments that instead of "acks" key might be any symbols, hence I would suggest to use next regexp (\{.*?:\")*(\"})* see https://regex101.com/r/kfAYsQ/1

System.out.println("[{\"key1\":\"6\"},{\"key2\":\"17\"},{\"key3\":\"888\"}]".replaceAll("(\\{.*?:\")*(\"})*", ""));

input: [{\"key1\":\"6\"},{\"key2\":\"17\"},{\"key3\":\"888\"}]

result: [6,17,888]

Abra
  • 19,142
  • 7
  • 29
  • 41
ikhot
  • 1
  • 2
  • That's JavaScript, not Java. – Barmar Jun 29 '23 at 16:58
  • you have posted "java regex with curly braces" and did "java" tag for the post. Hence I was thinking that it is Java :) – ikhot Jun 29 '23 at 17:02
  • I thought yuor answer was JavaScript. But they both have similar `replaceAll()` methods. – Barmar Jun 29 '23 at 17:05
  • BTW, I'm not the OP. – Barmar Jun 29 '23 at 17:05
  • sorry I have confused you with the author. Then everything is ok, my answer is written in Java. – ikhot Jun 29 '23 at 17:07
  • Why do you have two `"` in `[\"acks:\"]`. One of them will match all `"` characters. – Barmar Jun 29 '23 at 17:13
  • I have just little bit modified suggested regexp in the post, actually it can be inserted after quotes [\"acks\":][}]*", but both options work as expected. – ikhot Jun 29 '23 at 17:20
  • I mean it should be `[\"acks:]` it doesn't need the `"` after `:` – Barmar Jun 29 '23 at 17:20
  • 1
    `[]` is for character sets, it doesn't match strings. It could also be `[\":skca]` – Barmar Jun 29 '23 at 17:21
  • @Barmar "It could also be [\":skca]" - seems like you are trying to interpret the question in your way. First post contains clear question and there is no any note that it might be any symbols. Please feel free to post your own answer as well. – ikhot Jun 29 '23 at 17:30
  • You don't seem to understand how square brackets work in regular expressions. `[abc]` and `[cba]` are equivalent -- they match any of those 3 characters. – Barmar Jun 29 '23 at 17:31
  • And `[abc]*` will match `bca` because it matches any sequence of any of those characters. – Barmar Jun 29 '23 at 17:32
  • @IvanKhotynskyi, consider the following [demo](https://regex101.com/r/saFskD/1). Also, please, look into what [character class](https://stackoverflow.com/questions/1545751/how-to-back-reference-inner-selections-in-a-regular-expression/1553171#1553171) mean in regex, before attempting any further discussions on the matter. – markalex Jun 29 '23 at 17:36
  • @markalex not sure why you have rated negatively my answer, as it works. btw. your answer does not cover all cases, If to use this regexp [^\d,[\]]+ then it will not work correctly for keys that contain numbers. – ikhot Jun 29 '23 at 18:58
  • @ikhot, I downvoted your answer, because it showed clear lack of understanding how character classes work, and was misleading on this basis. And your "elegant way" is questionable at best. – markalex Jun 29 '23 at 19:08
  • Can you explain why you use `(\\{\"acks\":\")*(\"})*` instead of something like`\\{\"acks\":\"|\"}`? Also, no comments imply that keys might be something other than "acks" (It might very well be the case, but no such comment where made under your answer): comments were about initial incorrect use of character classes in your answer. – markalex Jun 29 '23 at 20:42