正则表达式!!!处理字符串

aino77 2008-07-18 03:37:23
这样一个字符串"B2+D2+AF2+G13+B4+400"
应该大家都见过,这是Excel中单元格的公式,其中的B2、D2...都是相应的单元格
其实想要得到的结果很简单,就是想把"其中表示单元格行的数字"加上一个常数

比如:+3
则:B2->B5、AF2->AF5、G13->G16
结果:"B5+D5+E5+AF5+G16+B7+400"

但我做起来比较费劲。感觉用正则比较好做。求一个vb函数。

'str是原来的字符串公式,AddNum是要加的常数

Private Function RowNumAdd(ByVal str As String, ByVal AddNum As Integer) As String
.......
End Function


先谢了。
...全文
105 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aino77 2008-07-18
  • 打赏
  • 举报
回复
Thank you 。劳烦大家了。
conan304 2008-07-18
  • 打赏
  • 举报
回复
再改:
namespace csdn
{
class Program
{
static void Main(string[] args)
{
string str = "B2+D2+AF2+G13+B4+400+C213+C1";
Console.WriteLine("string is:"+str);
Console.WriteLine("RowNumAdd is:"+RowNumAdd(str, 5));
}

private static string RowNumAdd(string str, int addNum)
{
foreach (Match m in Regex.Matches(str, @"[a-zA-Z]+[0-9]+"))
{
int i = Convert.ToInt32(Regex.Match(m.Value, @"[0-9]+").ToString()) + addNum;
str = str.Replace(m.Value, Regex.Match(m.Value, @"[a-zA-Z]+").Value + i.ToString());
}
return str;
}
}
}
/*
string is:B2+D2+AF2+G13+B4+400+C213+C1
RowNumAdd is:B7+D7+AF7+G18+B9+400+C218+C6
请按任意键继续. . .
*/
Kevin2328 2008-07-18
  • 打赏
  • 举报
回复

'strSrc= "B2+D2+AF2+G13+B4+400"
'Addnum=5
Private Function StepNum(ByVal strSrc As String, ByVal Addnum As Integer) As String
Dim digitregex As Regex = New Regex("[a-zA-Z]+[0-9]+", RegexOptions.RightToLeft)
Dim Before As String = strSrc
Dim strSub As String
Dim tmpM As Match

tmpM = digitregex.Match(Before)
While tmpM.Success
strSub = Regex.Match(tmpM.Value, "[a-zA-Z]+").Value & (Val(Regex.Match(tmpM.Value, "[0-9]+").Value) + Addnum).ToString
strSrc = strSrc.Substring(0, tmpM.Index) & strSub & strSrc.Substring(tmpM.Index + strSub.Length)
tmpM = tmpM.NextMatch
End While

Return strSrc
End Function
conan304 2008-07-18
  • 打赏
  • 举报
回复
Sorry,过于复杂
conan304 2008-07-18
  • 打赏
  • 举报
回复
菜鸟写的,觉得过于负责,等高手

string str = "B2+D2+AF2+G13+B4+400+C213";
string[] text=str.Split('+');
string temp = "";
foreach(string s in text)
{
if(Regex.IsMatch(s,"[a-zA-Z]+[0-9]+"))
{
temp+=Regex.Match(s,"[a-zA-Z]+").Value+(Convert.ToInt32(Regex.Match(s,@"[0-9]+").Value)+3).ToString()+"+";
}
else
{
temp+=s+"+";
}
}
temp = temp.Substring(temp.Length - 1, 1) == "+" ? temp.Remove(temp.Length - 1) : temp;
Console.WriteLine(temp);

/*
B5+D5+AF5+G16+B7+400+C216
请按任意键继续. . .
*/
lclc88com 2008-07-18
  • 打赏
  • 举报
回复

Private Function u(ByVal a As String) As String
Dim k As String = ""
For i As Integer = 1 To a.Length
k = k + o(a.Substring(i - 1, 1))
Next
Return k
End Function

Private Function o(ByVal b As String) As String
Dim r As New Regex("(^[1-9]d*$)")
If r.IsMatch(b) Then
If YN = "NO" Then
YN = "SZ"
'前面是数字
Return b
End If
If YN = "YES" Then
YN = "SZ"
'前面是数字
Return "3"
End If
Return ""
Else
Dim p As New Regex("(^[A-Za-z]+$)")
If p.IsMatch(b) Then
YN = "YES"
'前面是字母
Return b
Else
YN = "NO"
'前面是字符
Return b
End If
End If
End Function


lclc88com 2008-07-18
  • 打赏
  • 举报
回复
private void buttonX1_Click(object sender, EventArgs e)
{
label1.Text = "";
YN = "NO";
label1.Text= a(textBox1.Text);
}

private string u(string a)
{
string k = "";
for (int i = 1; i <= a.Length; i++)
{
k = k+ o(a.Substring(i - 1, 1));
}
return k;
}

private string o(string b)
{
Regex r = new Regex(@"(^[1-9]d*$)");
if (r.IsMatch(b))
{
if (YN == "NO")
{
YN = "SZ";//前面是数字
return b;
}
if (YN == "YES")
{
YN = "SZ";//前面是数字
return "3";
}
return "";
}
else
{
Regex p = new Regex(@"(^[A-Za-z]+$)");
if (p.IsMatch(b))
{
YN = "YES";//前面是字母
return b;
}
else
{
YN = "NO";//前面是字符
return b;
}
}
}









public void regexyn(string lsstr)
{
//Regex r = new Regex(@"([^a-zA-Z0-9_]+)");
Regex r = new Regex(@"(^[A-Za-z]+$)");
if (r.IsMatch(lsstr))
{
YN = "YES";
label1.Text = label1.Text + lsstr;
}
else
{
YN = "NO";
}
}
}

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧