仅在大括号外的空格上分割字符串 [英] split a string only on spaces that are outside curly braces

查看:57
本文介绍了仅在大括号外的空格上分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是regex的新手,我需要一些帮助.我阅读了一些与此问题相似的主题,但是我不知道该如何解决.

I am new to regex and I need some help. I read some topics similar to this issue, but I could not figure out how to resolve it.

我需要在不在一对大括号内的每个空白处分割一个字符串.大括号外的连续空格应视为一个空格:

I need to split a string on every blank space that is not inside a pair of curly braces. Consecutive blank spaces outside the curly braces shall be considered as a single one:

{ TEST test } test { test test} {test test  }   { 123 } test  test 

结果:

{ TEST test } 

test 

{ test test} 

{test test  }   

{ 123 } 

test  

test

推荐答案

\{[^}]+\}|\S+

这将匹配用花括号括起来的所有字符或非空格字符.从字符串中获取所有与之匹配的内容应该会为您提供所需的内容.

This matches either a run of any characters enclosed by curly braces, or a run of non-space characters. Grabbing all of the matches for it out of your string should provide you with what you want.

这篇关于仅在大括号外的空格上分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆