正则表达式 - 将引用的字符串拆分为一个单词 [英] Regex - split quoted string as one words

查看:91
本文介绍了正则表达式 - 将引用的字符串拆分为一个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑我在textbox1.text上有这样的字符串:

words1 words2 =hello world @!;



怎么样使用正则表达式将该字符串拆分为:

words1

words2

=

hello world @!

;



i知道使用此代码在正则表达式中使用空格分割字符串:

Consider i have string like this on textbox1.text:
words1 words2 = "hello world @!";

how to split that string using regex into :
words1
words2
=
"hello world @!"
;

i know to split string using whitespace in regex using this code :

string s = "there is a cat";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Split(' ');
foreach (string word in words)
{
    Console.WriteLine(word);
}





但如果使用上面的代码,输出将是:



words1

words2

=

你好

世界

@!;



如何提取引用的字符串作为一个单词?无论引用字符串中的空格多少并识别分号然后拆分分号?



我尝试过:



使用一些正则表达式模式\。*?\从字符串中提取引用的字符串..



but if use above code the output will be :

words1
words2
=
"hello
World
@!";

how to extract quoted string as one words? no matter how much the whitespace within the quoted string and recognize the semicolon then split semicolon?

What I have tried:

Using some regex pattern \".*?\" to extract quoted string from the string..

推荐答案

String.Split不是正则表达式 - 它只是一个基本的字符串方法,不能做任何太复杂的事情。

尝试:

String.Split isn't a Regex - it's just a basic string method that can't do anything too complex.
Try:
("[^"]*")|([^\s]+)

作为正则表达式 - 它应该为您提供您想要的单独组值。

as a regex - it should give you what you want as separate Group values.


这篇关于正则表达式 - 将引用的字符串拆分为一个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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