C# 将字符串分为两部分:大写和其他任何内容
我需要把一根绳子分成两部分。第一部分总是大写和数字。第二部分可以是任何东西C# 将字符串分为两部分:大写和其他任何内容,c#,regex,C#,Regex,我需要把一根绳子分成两部分。第一部分总是大写和数字。第二部分可以是任何东西 string pattern = "[A-Z]?[a-z]+"; string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss"; Match match = Regex.Match(toSplit, pattern); if (match.Success) { int ind
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
例如:
LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
应分为
LOREM IPSUM DOLOR 0,5 SIT
LOREM IPSUM DOLOR 0,5 SIT
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
及
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
此外:
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
应分为
LOREM IPSUM DOLOR 0,5 SIT
LOREM IPSUM DOLOR 0,5 SIT
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
及
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
您可以尝试以下方法:
Regex regex = new Regex(@"\b(?=[A-Z][a-z])");
string[] result = regex.Split(yourstr, 2);
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
图案详情:
\b # \b is a word boundary (limit between a member
# of the \w class and an other character
(?= # open a lookahead assertion that means "followed by"
[A-Z][a-z] # an Uppercase and a lowercase letter
)
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
split方法的第二个参数限制项目的数量
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
由于您已经编辑了问题,并且现在还希望在第一个小写字母处拆分,因此您可以将模式更改为:
Regex regex = new Regex(@"\b(?=[A-Z]?[a-z])");
Regex regex = new Regex(@"\s*\b(?=[A-Z]?[a-z])");
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
这使得大写字母是可选的
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
注意:如果要修剪第一个子字符串末尾的潜在尾随空格,可以将模式更改为:
Regex regex = new Regex(@"\b(?=[A-Z]?[a-z])");
Regex regex = new Regex(@"\s*\b(?=[A-Z]?[a-z])");
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
我将对此进行拆分:
(?),因为您已经提到它总是由两部分组成,这个简单的方法可以工作:
string text = " LOREM IPSUM DOLOR 0,5 SIT amet 0.3 consectetur adipiscing elit";
string[] words = text.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
bool startUpper = words.First().All(Char.IsUpper);
var firstSwitchingCaseWord = words
.Select((word, index) => new { word, index })
.FirstOrDefault(x => startUpper ? x.word.Any(Char.IsLower) : x.word.Any(Char.IsUpper));
if (firstSwitchingCaseWord != null)
{
string firstPart = string.Join(" ", words.Take(firstSwitchingCaseWord.index));
string lastPart = string.Join(" ", words.Skip(firstSwitchingCaseWord.index));
}
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
在以下正则表达式上拆分
(?<=^[A-Z0-9,\s]+)\s(?![A-Z0-9,]+\b)
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
另一种情况:
string input = "LOREM IPSUM DOLOR 0,5 SIT amet 0.3 consectetur adipiscing elit";
string[] parts = Regex.Split(input, @"(?<=^[A-Z0-9,\s]+)\s(?![A-Z0-9,]+\b)");
foreach (string part in parts)
Console.WriteLine(part);
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
你说第一个字符应该是大写、数字或符号。实际上,如果你要使用下面的字符,你必须指定ascii码。如果你把它和正则表达式混淆了,这是简单易懂的
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
目前,如果第一个字符是大写字符或数字,如果你想添加一个字符,你需要在中指定ascii值,如果方法中找到了条件,我就这样做了
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
试试下面的一个--
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
static void Main(字符串[]args)
{
字符串输入=“LOREM IPSUM DOLOR 0,5 SIT Amet concertetur adipsicing elit”;
字符串[]arr=input.Split(“”);
List splitList=新列表();
int loc=-1;
对于(int i=0;i64&&str[0]<91)| |(str[0]>47&&str[0]<58))
{
如果(str.Count(x=>(x>96&&x<123))>0)
{
结果=真;
}
}
返回结果;
}
以防您的字符串仅在第一次出现时被拆分
string pattern = "[A-Z]?[a-z]+";
string toSplit = "LOREM IPSUM DOLOR 0,5 SIT Amet consectetur adipiscing elit Atest sss";
Match match = Regex.Match(toSplit, pattern);
if (match.Success) {
int index = match.Index;
string splittedVal = toSplit.Substring(0, index);
string secondPart = toSplit.Substring(index, toSplit.Length - index);
}
是否总是两部分?如果不是,第三部分什么时候开始,在前两个大写字母?你做了什么努力?第一部分可以包含哪些字符?我已经编辑了问题,可以回顾一下答案吗?