使用 Java 中的正则表达式替换多个值

huangapple go评论76阅读模式
英文:

Replace multiple value by using regex in Java

问题

我正在使用 MongoDB 和 Spring Boot。当我执行查询时,可以在终端中获得类似以下的查询日志。

[{ "$match" : { "$and" : [{ "type" : "VIDEO"}, { "excludeUserIds" : { "$not" : { "$elemMatch" : { "$and" : [{ "_id" : 23}, { "groupIds" : { "$in" : [{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}.......]

为了检查是否正常工作,我需要复制查询并粘贴到Mongo Compass中,Mongo Compass是一个可以在多个阶段执行查询的工具。

但问题是,[{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}] 这种格式不能在Mongo Compass中使用。代替它,我们必须使用 ObjectId 作为一个包装器,如下所示(类似于将 { "$oid" : 替换为 ObjectId(")。

[ObjectId("5f434c9b51a7ff3214158a42"), ObjectId("5f113ed4fd775a238ee5ce59")]

我一次在多个地方获取超过50个 ObjectId。我的计划是使用正则表达式查找每个对象({ "$oid" : "the_value"})并替换为 ObjectId("the_value")

正则表达式 \{\s\"([a-zA-Z\\d$]+)\"\s:\s\"([^\"]*)\"} 可以找到所有对象。但我在替换相同值的地方遇到了困难。

所以我写了一个程序:

public static void main(String[] args) throws IOException {
    
    BufferedReader reader = new BufferedReader(new FileReader(System.getProperty("user.home") + "/Desktop/input.txt"));
    FileWriter writer = new FileWriter(System.getProperty("user.home") + "/Desktop/output.txt");
    String str = "";

    try {
        while ((str = reader.readLine()) != null) {
            final Pattern p = Pattern.compile("\\{\\s\\\"([a-zA-Z\\\\d$]+)\\\"\\s:\\s\\\"([^\\\"]*)\\\"}");
            Matcher m = p.matcher(str);

            if (m.find()) {
                str = m.replaceAll("ObjectId('$" + m.group(2) + "')");
            }
            writer.write(str);
        }
    } catch (Exception e) {
        System.out.println("File not found");
    }
    writer.close();
    reader.close();
}

更新 1

现在我得到的是 ........{ "$in" : [ObjectId('VIDEO'), ObjectId('VIDEO')。这并不能得到我想要的确切值(5f434c9b51a7ff3214158a425f113ed4fd775a238ee5ce59)。

英文:

I'm working with MongoDB and Spring Boot. When I execute a query, I can get the query log in the terminal like following.

[{ "$match" : { "$and" : [{ "type" : "VIDEO"}, { "excludeUserIds" : { "$not" : { "$elemMatch" : { "$and" : [{ "_id" : 23}, { "groupIds" : { "$in" : [{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}.......

To check whether its working fine or not, I need to copy the query and paste the above query in Mongo compass which is a tool that helps to execute queries in multiple stages.

But the problem is, [{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}] this format cannot be used in the Mongo compass. Instead of that, we have to use ObjectId as a wrapper like following. (Kind of replacing on { "$oid" : )

[ObjectId("5f434c9b51a7ff3214158a42"), ObjectId("5f113ed4fd775a238ee5ce59")]

I'm getting more than 50 ObjectId at a time in multiple places. My plan is to find each and every objects ({ "$oid" : "the_value"}) using regex and replace by ObjectId("the_value").

The regex \{\s\"([a-zA-Z\\d$]+)\"\s:\s\"([^\"]*)\"} is finding all the objects. But I'm struggling at replacing the same value on the place.

So I wrote a program:

public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new FileReader(System.getProperty("user.home") + "/Desktop/input.txt"));
        FileWriter writer = new FileWriter(System.getProperty("user.home") + "/Desktop/output.txt");
        String str = "";

        try {
            while ((str = reader.readLine()) != null) {
                final Pattern p = Pattern.compile("\\{\\s\\\"([a-zA-Z\\\\d$]+)\\\"\\s:\\s\\\"([^\\\"]*)\\\"}");
                Matcher m = p.matcher(str);

                if (m.find()) {
                    str = m.replaceAll("ObjectId('" + m.group(2) + "')");
                }
                writer.write(str);
            }
        } catch (Exception e) {
            System.out.println("File not found");
        }
        writer.close();
        reader.close();
    }

Update 1

Now I'm getting ........{ "$in" : [ObjectId('VIDEO'), ObjectId('VIDEO'). That doesn't get the exact value (5f434c9b51a7ff3214158a42, 5f113ed4fd775a238ee5ce59) I want.

答案1

得分: 1

如果您愿意使用库,gson 似乎是适合您情况的完美选择。使用 gson,数据将被解析为一个映射(map)。
您可以使用该映射构造出站数据,并以您自定义的格式呈现。

英文:

If you are open to using libraries, gson seems like the perfect match for your situation. Using gson, data would be parsed into a map.
You can use the map and construct outbound data in your custom format.

答案2

得分: 1

我认为这应该有所帮助。随意编辑我的答案,这可能对其他人有帮助。

sed -E 's/\{\s\"([a-zA-Z\\d$]+)\"\s:\s\"([^"]*)\"}/ObjectId()/g' myFile.txt

使用 Java 中的正则表达式替换多个值

这是分组和替换(基于标签)。在这里,\1 代表第一组($oid),\2 代表第二组(objectId)。

然而,如果你想要使用 Java 程序本身。
我在你的 Java 程序中看到的问题是 m.group(2) 被用在了 m.replaceAll() 中。
正如你所说,会有超过 50 个匹配,所以 m.find() 会为 50 次返回 true,但你只替换了一次匹配的文本为 m.group(2),所以这所有的 50 个出现都应该被第一个匹配的值替换。(在你的情况下是 VIDEO,我不知道具体内容,需要查看你的文件)

我做了一个简单的程序,这应该也有所帮助。
同样,随意编辑或评论。

public static void main(String[] args) {

    String str = "[{ \"$oid\" : \"5f434c9b51a7ff3214158a42\"}, { \"$oid\" : \"5f113ed4fd775a238ee5ce59\"}]";
    final Pattern p = Pattern.compile("\\{\\s*\"([a-zA-Z\\\\d$]+)\"\\s*:\\s*\"([^\"]*)\"}");
    Matcher m = p.matcher(str);

    String finalString = "[";
    while (m.find()) {
        finalString += "ObjectId('" + m.group(2) + "') ";
    }
    finalString += "]";
    System.out.println(finalString);
}
英文:

I think this should Help. Feel Free to edit my answer it may help someone else.

 sed -E 's/\{\s\"([a-zA-Z\\d$]+)\"\s:\s\"([^\"]*)\"}/ObjectId()/g' myFile.txt

使用 Java 中的正则表达式替换多个值

This is grouping and replacing (tag based). Here \1 will represt the first group i.e. ($oid) and \2 represent the second group (objectId)

However if you would like to use the java program itself.
The Problem I see in your java program is that m.group(2)
is used with m.replaceAll(). As you said there are going to be more than 50 matches so m.find() will be true for 50 times but you are only replacing the matched text with m.group(2) once, so all those 50 occurrences should be replaced by the first matched value. ( In your case it is VIDEO I don't know how that is have to see your file for that)

I have made a simple program it should help as well.
Again feel free to edit or comment.

public static void main(String[] args) {

    String str = "[{ \"$oid\" : \"5f434c9b51a7ff3214158a42\"}, { \"$oid\" : \"5f113ed4fd775a238ee5ce59\"}]";
    final Pattern p = Pattern.compile("\\{\\s\\\"([a-zA-Z\\\\d$]+)\\\"\\s:\\s\\\"([^\\\"]*)\\\"}");
    Matcher m = p.matcher(str);

    String finalString = "[";
    while (m.find()) {
        finalString += "ObjectId('" + m.group(2) + "') ";
    }
    finalString += "]";
    System.out.println(finalString);
}

答案3

得分: 1

public static void main(String[] args) throws IOException {
    String file = System.getProperty("user.home") + "/Desktop/input.txt";
    String input = new String(Files.readAllBytes(Paths.get(file)));
    System.out.println(input);

    // { "$oid" : "the_value"} replaced by ObjectId("the_value")
    String regex = "\\{\\s\"\$oid\"\\s:\\s\"(\\w+)\"\\}";
    String output = input.replaceAll(regex, "ObjectId(\"$1\")");
    System.out.println(output);
}
Input:

    [{ "$match" : { "$and" : [{ "type" : "VIDEO"}, { "excludeUserIds" : { "$not" : { "$elemMatch" : { "$and" : [{ "_id" : 23}, { "groupIds" : { "$in" : [{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}]

Output:

    [{ "$match" : { "$and" : [{ "type" : "VIDEO"}, { "excludeUserIds" : { "$not" : { "$elemMatch" : { "$and" : [{ "_id" : 23}, { "groupIds" : { "$in" : [ObjectId("5f434c9b51a7ff3214158a42"), ObjectId("5f113ed4fd775a238ee5ce59")]

Summary Result:

    [{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}]

Was changed to:

    [ObjectId("5f434c9b51a7ff3214158a42"), ObjectId("5f113ed4fd775a238ee5ce59")]
英文:

Replace All with Regex - substitute with Variable and multiple value

Alternative Regex:

"\\{\\s\"\$oid\"\\s:\\s\"(\\w+)\"\\}"

Regex in context:

public static void main(String[] args) throws IOException {
    String file = System.getProperty("user.home") + "/Desktop/input.txt";
    String input = new String(Files.readAllBytes(Paths.get(file)));
    System.out.println(input);

    // { "$oid" : "the_value"} replaced by ObjectId("the_value")
    String regex = "\\{\\s\"\$oid\"\\s:\\s\"(\\w+)\"\\}";
    String output = input.replaceAll(regex, "ObjectId(\"$1\")");
    System.out.println(output);
}

Input:

[{ "$match" : { "$and" : [{ "type" : "VIDEO"}, { "excludeUserIds" : { "$not" : { "$elemMatch" : { "$and" : [{ "_id" : 23}, { "groupIds" : { "$in" : [{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}]

Output:

[{ "$match" : { "$and" : [{ "type" : "VIDEO"}, { "excludeUserIds" : { "$not" : { "$elemMatch" : { "$and" : [{ "_id" : 23}, { "groupIds" : { "$in" : [ObjectId("5f434c9b51a7ff3214158a42"), ObjectId("5f113ed4fd775a238ee5ce59")]

Summary Result:

[{ "$oid" : "5f434c9b51a7ff3214158a42"}, { "$oid" : "5f113ed4fd775a238ee5ce59"}]

Was changed to:

[ObjectId("5f434c9b51a7ff3214158a42"), ObjectId("5f113ed4fd775a238ee5ce59")]

huangapple
  • 本文由 发表于 2020年9月3日 14:07:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63717780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定