110,579
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
X(7);
}
private void X(int a)
{
string str = "if(d3+(j9+m88)/8+43+2+Round(hh99+jj9))";
MatchCollection matches = Regex.Matches(str, @"[a-z]+\d+");
foreach (Match match in matches)
{
Match matchSZ = Regex.Match(match.Value, @"\d+");
Match matchYW = Regex.Match(match.Value, "[a-z]+");
str = str.Replace(match.Value,matchYW.Value+ Convert.ToString(int.Parse(matchSZ.Value) + a));
}
Console.WriteLine(str);
}
}
}
Regex reg = new Regex(@"(?i)\b[a-z]+\d+\b");
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "if(d3+(j9+m88)/8+43+2+Round(hh99+jj9))";
Regex reg = new Regex(@"(?i)\b([a-z]+)(\d+)\b");
MatchCollection mc = reg.Matches(str);
foreach (Match m in mc)
{
string istr = Regex.Replace(m.Value, "(?i)[a-z]+", "");
string sstr = Regex.Replace(m.Value, @"\d+", "");
str = str.Replace(m.Value, sstr + (Convert.ToInt32(istr) + 7).ToString());
}
Console.WriteLine(str);
}
}
}
String a = "if(d3+(j9+m88)/8+43+2+Round(hh99+jj9))";
bool match = false;
StringBuilder tmp = new StringBuilder();
StringBuilder result = new StringBuilder();
foreach (char c in a)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
match = true;
}
if (c >= '0' && c <= '9')
{
if (match)
{
tmp.Append(c);
}
else
{
result.Append(c);
}
}
else if (tmp.Length == 0)
{
result.Append(c);
}
else
{
match = false;
result.Append(int.Parse(tmp.ToString()) + 7);
tmp.Remove(0, tmp.Length);
result.Append(c);
}
}
MessageBox.Show(result.ToString());
string srcString = "if(d3+(j9+m88)/8+43+2+Round(hh99+jj9))";
Regex r = new Regex("[a-zA-Z]+[0-9]+", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = r.Matches(srcString);
for (int i = 0; i < matches.Count; i++)
{
//string str1 = matches[i].ToString();//取得以字母开始数字结尾的字符,如d3,j9,m88,hh99,jj9
//string str2 = Regex.Replace(matches[i].ToString(), @"[a-zA-Z]+", "");//将str1字符中的字母抹去
int newnum = int.Parse(Regex.Replace(matches[i].ToString(), @"[a-zA-Z]+", "")) + 7;//将str1字符中的数字加7
string str3 = Regex.Replace(matches[i].ToString(), @"[0-9]+", newnum.ToString());//新得到的数字替换str1中的数字
srcString = srcString.Replace(matches[i].ToString(), str3);
}