通过匹配给定字符串中的模式来获取字符串数组(Get the array of strings by matching the pattern in the given string)
我有一个模式
@@{}
并给出一个字符串,我需要找出大括号之间的所有字符串。示例:如果我的字符串为
Hi This is @@{first} and second is @@{second} along with third @@{third} string
我期望的输出是一个由元素组成的字符串数组:
first second third
我的Java代码如下:
Pattern p = Pattern.compile("\\@\\@\\{(.+?)\\}"); Matcher match = p.matcher("Hi This is @@{first} and second is @@{second} along" + "with third @@{third} string"); while(match.find()) { System.out.println(match.group()); }
但我得到的输出是
@@{first} @@{second} @@{third}
请指导我如何获得所需的输出以及我正在做的错误
I have a pattern
@@{}
and given a string I need to find out all the strings coming in between the curly braces.Example : If my string is
Hi This is @@{first} and second is @@{second} along with third @@{third} string
The output I expect is a string array consisting of elements:
first second third
My Java code for this goes as :
Pattern p = Pattern.compile("\\@\\@\\{(.+?)\\}"); Matcher match = p.matcher("Hi This is @@{first} and second is @@{second} along" + "with third @@{third} string"); while(match.find()) { System.out.println(match.group()); }
But the output which I am getting is
@@{first} @@{second} @@{third}
Please guide me how to get the desired output and what mistake I am doing
原文:https://stackoverflow.com/questions/1706199
最满意答案
您的代码有两个问题:
第一个是你在
when
语句中检查(rest coll)
而不是(rest x)
,导致无限循环。第二个是
map
返回一个惰性序列,这意味着没有发生对connections
的调用。 这可以通过使用doall
将调用包装到map
来修复,但是由于您对副作用而不是返回值感兴趣,所以应该使用doseq
。这个版本的函数(多余的
loop
也已被删除)应该工作:(defn make-conts [coll] (when (not (empty? (rest coll))) (doseq [endpoint (rest coll)] (connections (first coll) endpoint)) (recur (rest coll))))
您可以使用
clojure.math.combinatorics
的组合,而不是自己编写代码来生成组合:(require '[clojure.math.combinatorics :as combi]) (defn make-conts [coll] (doseq [[p1 p2] (combi/combinations coll 2)] (connections p1 p2)))
There are two problems with your code:
The first is that you are checking
(rest coll)
instead of(rest x)
in thewhen
statement, leading to an infinite loop.The second is that
map
returns a lazy sequence, meaning that the calls toconnections
is not taking place. This can be fixed by wrapping the call tomap
withdoall
, but as you are interested in the side effects, not the return values, you should usedoseq
instead.This version of the function (where the superfluous
loop
has also been removed) should work:(defn make-conts [coll] (when (not (empty? (rest coll))) (doseq [endpoint (rest coll)] (connections (first coll) endpoint)) (recur (rest coll))))
Instead of writing the code to generate the combinations yourself, you can use
combinations
fromclojure.math.combinatorics
:(require '[clojure.math.combinatorics :as combi]) (defn make-conts [coll] (doseq [[p1 p2] (combi/combinations coll 2)] (connections p1 p2)))
相关问答
更多-
1)当我假设下面的代码表示绘制棋盘方块的顺序是从左到右垂直向下直到绘制整个棋盘时,我的理解是否正确? 这是对的。 逐行,从左到右,从上到下。 2)我对double x的目的有一些疑问,在内部最for for循环中有双y。 它们的计算是为了将一个方格间隔到下一个方格吗? 你能否扩展双x和双y的目的以及一个循环的每个循环会发生什么? 它们是绘制下一个正方形的位置的坐标。 具体来说,它们是正方形左上部分的坐标。 您可以通过简单地乘以正方形的宽度来计算所有正方形的起点,就像它们已经完成的那样。 1) Is my u ...
-
对于clojure中的循环(For loop in clojure)[2022-11-18]
不知道如何创建ISeq:java.lang.Long clojure.lang.RT.seqFrom 此错误消息准确地说明了异常的原因。 我猜你通过了预期seq的long值。 我可以重现它: user> (def v [1 2 3]) #'user/v user> (defn foo [arg] (for [ i (range (count v)) ] (for [j arg ] [i j]))) #'user/foo user ... -
如果我解释得好,那可能吗? 你需要使用另一个matrix4,如果使用box2d,例如PPM单位。 shapeRenderer.setProjectionMatrix(camera.combined); 改变例如: testMatrix4 Matrix4; //<-- Variable Class. //在你的代码中 //Other Code //batch or Camera Matrix testMatrix4 = batch.getProyectionMatrix().cpy() ...
-
问题是我没有激活“fun(ctional)模式”,因此draw函数需要取0个参数,因为默认状态是全局管理的。 这有效: (ns quil-test.quil-first (:require [helpers.general-helpers :as g] [quil.core :as q] [quil.middleware :as m]) (:gen-class)) (defn setup-state [] (q/frame-rate 60) {:x ...
-
第一个for循环打印左边的填充(所有空格)。 表达式lines - i - 1来自于您需要居中树的事实,并且您知道第0行具有单个x ,第1行具有3( xxx ),第2行具有5( xxxxx ),依此类推。 但是左侧空格的数量取决于树的总行数,因此您需要考虑这些lines以及当前线的索引(这是变量i的值)。 要找出这两者之间的关系,你可以尝试使用少量级别,让我们说3: __x 2 for the 1st line. _xxx 1 for the 2nd line. xxxxx 0 for t ...
-
Clojure国家如何运作?(How does Clojure state work?)[2023-07-16]
简短的回答:它使用applet.clj中定义的原子 它是第17行core.clj中定义的函数,它提取存储为附加到applet 上元数据的原子的状态信息。 通过调用(:state (meta (current-applet))查找存储当前“状态”的原子来设置状态,从而从applet对象中提取元数据(这是一个映射),然后返回存储在以下内容中的原子:然后调用reset!在这个原子中放入一个新值。它由ns声明中的(:use quil.core)包含。 the short answer: it's using an ... -
我选择了一张随机图片,将其重命名为bill.jpg并将其放在项目根目录下,您的代码完美无缺。 以下是我认为相关的文件及其位置: ~/Projects/repl/project.clj ~/Projects/repl/bill.jpg ~/Projects/repl/src/img.clj I picked a random picture, renamed it to bill.jpg and put it under the project root and your code worked perfe ...
-
你所采取的时间并不是特别有意义,因为代码还不热。 你需要“预热”代码,以便JVM将JIT编译它,那就是你应该开始看到好的速度。 您应该看看如何在Clojure中对功能进行基准测试? (你应该使用Criterium 。) 至于你的代码,你正在使用数组,所以这应该会给你带来良好的性能。 风格方面,你有两个悬挂]真的很奇怪。 也许那只是格式化错误? 消除尽可能多的重复代码通常很好,所以我也改变了这一点 (if (pxl-over-threshold? idx) (aset pxls idx (color 25 ...
-
您的代码有两个问题: 第一个是你在when语句中检查(rest coll)而不是(rest x) ,导致无限循环。 第二个是map返回一个惰性序列,这意味着没有发生对connections的调用。 这可以通过使用doall将调用包装到map来修复,但是由于您对副作用而不是返回值感兴趣,所以应该使用doseq 。 这个版本的函数(多余的loop也已被删除)应该工作: (defn make-conts [coll] (when (not (empty? (rest coll))) (dose ...
-
如何使用Emacs / nrepl / Quil在Clojure中实现代码?(How do I live code in Clojure using Emacs / nrepl / Quil?)[2021-07-28]
在要缓存的函数中尝试CMx (这会破坏当前的顶级表单)或者在源缓冲区中使用Cc Ck (这会破坏当前缓冲区)。 顺便说一句, Cx Ce也应该工作(它肯定适合我,但我很少使用它)。 也许你没有使用nrepl.el的最新版本? Try C-M-x (this evals the current top-level form) in the function you want to change or C-c C-k (this evals the current buffer) in the source b ...