首页 \ 问答 \ 通过匹配给定字符串中的模式来获取字符串数组(Get the array of strings by matching the pattern in the given string)

通过匹配给定字符串中的模式来获取字符串数组(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
更新时间:2021-11-02 13:11

最满意答案

您的代码有两个问题:

第一个是你在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 the when statement, leading to an infinite loop.

The second is that map returns a lazy sequence, meaning that the calls to connections is not taking place. This can be fixed by wrapping the call to map with doall, but as you are interested in the side effects, not the return values, you should use doseq 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 from clojure.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 ...
  • 不知道如何创建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 ...
  • 简短的回答:它使用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 ...
  • 在要缓存的函数中尝试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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)