for (int i = 0; i < strArr.Length; i++) { strArr[i] = strArr[i].Replace(".", " "); listBox1.Items.Add(strArr[i]); }
string s = "aa \"ha ha\" ss"; MatchCollection mc = Regex.Matches(s, @"""([^""]+)""");s = Regex.Replace(s, @"""[^""]+""", ""); string[] sp = Regex.Split(s, @"\s+"); foreach (string str in sp) { Console.WriteLine(str); } for (int i = 0; i < mc.Count; i++) { Console.WriteLine(mc[i].Groups[1]); }Console.Read();
static void Main(string[] args) { string strText = "a \"ha ha\" ws \"sdf s d\" bbb"; Regex r = new Regex("\"[^\"]*\""); foreach (Match item in r.Matches(strText)) { strText = strText.Remove(strText.IndexOf(item.Groups[0].Value), item.Groups[0].Value.Length); Console.WriteLine(item.Groups[0].Value); } r = new Regex("(\\w*)"); foreach (Match item in r.Matches(strText)) { Console.WriteLine(item.Groups[0].Value); }
Console.Read();
}只会分两次写。一次的不会写
//仅供参考 String str=@"a ""ha ha"" sds "" t e s t "" s""ws"; Regex objRegex = new Regex(@"(?i)(?:""(.+?)"")|(?:([^\s]+))"); MatchCollection objMatches = objRegex.Matches(str); Response.Write("Result :<br />"); Int32 index = 0; foreach (Match objMatch in objMatches) { Response.Write(String.Format("{0:2} : {1}<br />", ++index, objMatch.Result("$1$2"))); } //结果: /* Result : 2 : a 2 : ha ha 2 : sds 2 : t e s t 2 : s"ws */
string pattern = " \"(?<Info>.*)\" "; MatchCollection matchCollection = Regex.Matches(text, pattern); List<string> resultList = new List<string>(); int currentIndex = 0; foreach(Match match in matchCollection) { if(match.Index > currentIndex) { resultList.Add(text.Substring(currentIndex,match.Index-currentIndex+1).Trim()); currentIndex = match.Index; } resultList.Add(match.Groups["Info"].Value); }resultList就是你想要的。你调试一下,看看,我随手敲的,里面应该有一些错误,但基本思路如此。
private void button28_Click(object sender, EventArgs e) { string text = "aa \"ha ha\" ss \"ff ddf sf\" a"; string pattern = string.Format("{0}(?<Info>.*){1}", "\n", "\n"); MatchCollection matchCollection = Regex.Matches(text.Replace("\"","\n"), pattern,RegexOptions.Multiline); List<string> resultList = new List<string>(); int currentIndex = 0; foreach (Match match in matchCollection) { if (match.Index > currentIndex) { resultList.Add(text.Substring(currentIndex, match.Index - currentIndex).Trim()); currentIndex = match.Index; } resultList.Add(match.Groups["Info"].Value); currentIndex += match.Length; } resultList.Add(text.Substring(currentIndex).Trim()); foreach(string result in resultList) { MessageBox.Show(result); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program {
static void Main(string[] args) { string s = "a \"ha ha\" ws \"ha ha\" ws"; s = s + " "; MatchCollection Ms1 = Regex.Matches(s, "\"(.*)\"", RegexOptions.Multiline| RegexOptions.IgnoreCase); foreach (Match m in Ms1) { s=s.Replace(m.Groups[1].Value,m.Groups[1].Value.Replace(" ","-")); } s=Regex.Replace(s, " ", "\r\n");
string[] strs = str.Split('\"');
strs[0].Trim(), strs[1].Trim().Replace("\"",""), strs[2].Trim()就是你想要的东东了
最后按index去截取。试试看看
bool start = false;
for (int i=0;i<str.Length;i++)
{
string one = str.Substring(i, 1);
if (one == " ")
{
if (start)
{
str = str.Remove(i, 1);
str = str.Insert(i, ".");
}
}
if (one == "\"")
{
start = !start;
}
}
str = str.Replace("\"", string.Empty);
string[] strArr= str.Split(new char[] { ' ' });
for (int i = 0; i < strArr.Length; i++)
{
strArr[i] = strArr[i].Replace(".", " ");
listBox1.Items.Add(strArr[i]);
}
MatchCollection mc = Regex.Matches(s, @"""([^""]+)""");s = Regex.Replace(s, @"""[^""]+""", "");
string[] sp = Regex.Split(s, @"\s+");
foreach (string str in sp)
{
Console.WriteLine(str);
}
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine(mc[i].Groups[1]);
}Console.Read();
{
string strText = "a \"ha ha\" ws \"sdf s d\" bbb";
Regex r = new Regex("\"[^\"]*\""); foreach (Match item in r.Matches(strText))
{
strText = strText.Remove(strText.IndexOf(item.Groups[0].Value), item.Groups[0].Value.Length);
Console.WriteLine(item.Groups[0].Value);
}
r = new Regex("(\\w*)");
foreach (Match item in r.Matches(strText))
{
Console.WriteLine(item.Groups[0].Value);
}
Console.Read();
}只会分两次写。一次的不会写
//仅供参考
String str=@"a ""ha ha"" sds "" t e s t "" s""ws"; Regex objRegex = new Regex(@"(?i)(?:""(.+?)"")|(?:([^\s]+))"); MatchCollection objMatches = objRegex.Matches(str); Response.Write("Result :<br />"); Int32 index = 0; foreach (Match objMatch in objMatches)
{
Response.Write(String.Format("{0:2} : {1}<br />", ++index, objMatch.Result("$1$2")));
}
//结果:
/*
Result :
2 : a
2 : ha ha
2 : sds
2 : t e s t
2 : s"ws
*/
MatchCollection matchCollection = Regex.Matches(text, pattern);
List<string> resultList = new List<string>();
int currentIndex = 0;
foreach(Match match in matchCollection)
{
if(match.Index > currentIndex)
{
resultList.Add(text.Substring(currentIndex,match.Index-currentIndex+1).Trim());
currentIndex = match.Index;
} resultList.Add(match.Groups["Info"].Value);
}resultList就是你想要的。你调试一下,看看,我随手敲的,里面应该有一些错误,但基本思路如此。
{
string text = "aa \"ha ha\" ss \"ff ddf sf\" a";
string pattern = string.Format("{0}(?<Info>.*){1}", "\n", "\n");
MatchCollection matchCollection = Regex.Matches(text.Replace("\"","\n"), pattern,RegexOptions.Multiline);
List<string> resultList = new List<string>();
int currentIndex = 0;
foreach (Match match in matchCollection)
{
if (match.Index > currentIndex)
{
resultList.Add(text.Substring(currentIndex, match.Index - currentIndex).Trim());
currentIndex = match.Index;
} resultList.Add(match.Groups["Info"].Value);
currentIndex += match.Length;
} resultList.Add(text.Substring(currentIndex).Trim()); foreach(string result in resultList)
{
MessageBox.Show(result);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string s = "a \"ha ha\" ws \"ha ha\" ws";
s = s + " "; MatchCollection Ms1 = Regex.Matches(s, "\"(.*)\"", RegexOptions.Multiline| RegexOptions.IgnoreCase);
foreach (Match m in Ms1)
{
s=s.Replace(m.Groups[1].Value,m.Groups[1].Value.Replace(" ","-"));
}
s=Regex.Replace(s, " ", "\r\n");
MatchCollection Ms2 = Regex.Matches(s, "(.*)\r\n", RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match M in Ms2)
{
Console.WriteLine(M.Groups[1].Value.Replace("-"," "));
}
Console.ReadLine(); }
}
}