Android 正则表达式
例如,我有这样的字符串 309\306\308\337\u 338 309\306\337\u 338 310 311\315\u 316\336\u 337 311\315\u 316\336\u 337 311\335_336 这些字符串表示页码列表,例如字符串“309\306\308\337_339”表示 第309306308337338339页 我想将这些字符串中的一个传递给函数,函数将其作为如下字符串返回 309306308337338339 这个函数可以做到这一点,但在c#中,我想在android中实现刺穿Android 正则表达式,android,regex,Android,Regex,例如,我有这样的字符串 309\306\308\337\u 338 309\306\337\u 338 310 311\315\u 316\336\u 337 311\315\u 316\336\u 337 311\335_336 这些字符串表示页码列表,例如字符串“309\306\308\337_339”表示 第309306308337338339页 我想将这些字符串中的一个传递给函数,函数将其作为如下字符串返回 309306308337338339 这个函数可以做到这一点,但在c#中,我想在
private static string Get_PageNumbers(string str)
{
ArrayList arrAll = new ArrayList();
MatchCollection match;
string[] excar;
string strid, firstNumber, lastlNumber;
int fn, ln;
ArrayList arrID = new ArrayList();
//***In Case The Range Number Between "_"
if (str.Contains("_"))
{
// match_reg = new Regex("(w?[\\d]+)*(_[\\d]+)");
Regex matchReg = new Regex("(w?[\\69]+_[\\d]+)*(q?[\\d]+//)*(a?[\\d]+_[\\d]+)*(y?[\\d]+)*");
match = matchReg.Matches(str);
int count = match.Count;
excar = new string[0];
for (int i = 0; i < count; i++)
{
Array.Resize(ref excar, count);
excar[i] = match[i].Groups[0].Value;
if (excar[i] != string.Empty)
arrID.Add(excar[i]);
}
//******IF Array Contains Range Of Number Like"102_110"
if (str.Contains("_"))
{
for (int i = 0; i < arrID.Count; i++)
{
strid = arrID[i].ToString();
if (arrID[i].ToString().Contains("_"))
{
int idy = strid.LastIndexOf("_");
firstNumber = strid.Substring(0, idy);
if (idy != -1)
{
lastlNumber = strid.Substring(idy + 1);
fn = int.Parse(firstNumber);
arrAll.Add(fn);
ln = int.Parse(lastlNumber);
for (int c = fn; c < ln; c++)
{
fn++;
arrAll.Add(fn);
}
}
}
else
{
arrAll.Add(arrID[i].ToString());
}
}
//******If Array Contain More Than One Number
if (arrAll.Count > 0)
{
str = "";
for (int i = 0; i < arrAll.Count; i++)
{
if (str != string.Empty)
str = str + "," + arrAll[i];
else
str = arrAll[i].ToString();
}
}
}
}
//***If string Contains between "/" only without "_"
else if (str.Contains("/") && !str.Contains("_"))
{
str = str.Replace("/", ",");
}
else if (str.Contains("\\"))
{
str = str.Replace("\\", ",");
}
return str;
}
private static string Get_页码(string str)
{
ArrayList arrAll=新的ArrayList();
匹配集合匹配;
字符串[]excar;
字符串strid、firstNumber、lastlNumber;
int-fn,ln;
ArrayList arrID=新的ArrayList();
//***如果范围数在“\u1”之间
if(str.Contains(“”))
{
//match_reg=新正则表达式(“(w?[\\d]+)*([\\d]+”);
Regex matchReg=new Regex(“(w?[\\69]+\\\d]+)*(q?[\\d]+/)*(a?[\\d]+\\\d]+)*(y?[\\d]+)*”;
match=matchReg.Matches(str);
int count=match.count;
excar=新字符串[0];
for(int i=0;i0)
{
str=“”;
for(int i=0;i
我认为使用split
功能更容易做到这一点:
public static String Get_PageNumbers(String str) {// Assume str = "309\\306\\308\\337_338"
String result = "";
String[] pages = str.split("\\\\"); // now we have pages = {"309","306","308","337_338"}
for (int i = 0; i < pages.length; i++) {
String page = pages[i];
int index = page.indexOf('_');
if (index != -1) { // special case i.e. "337_338", index = 3
int start = Integer.parseInt(page.substring(0, index)); // start = 337
int end = Integer.parseInt(page.substring(index + 1)); // end = 338
for (int j = start; j <= end; j++) {
result += String.valueOf(j);
if (j != end) { // don't add ',' after last one
result += ",";
}
}
} else { // regular case i.e. "309","306","308"
result += page;
}
if (i != (pages.length-1)) { // don't add ',' after last one
result += ",";
}
}
return result; // result = "309,306,308,337,338"
}
返回:
309,306,308,337,338
311,315,316,336,337
310
如果我能提出不同的实施方案
str.split(\\”)拆分字符串代码>,这里您将收到一个带有单个数字的数组字符串或类似“num\u num”的模式
str.split”(“”)代码>,现在您有了一个2位置数组
你的问题是什么?在C和C之间进行翻译并不难Java@MorrisonChang aim初学者@android我正在尝试,但代码崩溃,并且有很多例外情况,例如,谢谢你,Caner,但有问题,因为当我尝试s.replaceAll(“\”,“\”)时,字符串在数据库中保存时只有一个反斜杠“311\315\U 316\336\U 337”;它不接受申请,因为它将“311\315_316\336_337”解读为“311Íu 316Þu 337”,我也尝试对字符串进行编码,并对编码结果“311%C3%8D_316%C3%9E_337”进行解码,但解码结果仍然是“311Íu 316Þu 337”?????如何解决一个反斜杠的问题?????
309,306,308,337,338
311,315,316,336,337
310