- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有以下字符串:
Hellotoevryone<img height="115" width="150" alt="" src="/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" />Iamsogladtoseeall.
这个字符串表示一系列没有空格分隔的字符,在这个字符串中还插入了一个html图像。现在我想把字符串分成单词,每个单词的长度为 10 个字符,所以输出应该是:
1)Hellotoevr
2)yone<img height="115" width="150" alt="" src="/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg" />Iamsog
3)ladtoseeal
4)l.
所以想法是将任何 html 标记内容保留为 0 长度字符。
我写过这样的方法,但是没有考虑html标签:
public static string EnsureWordLength(this string target, int length)
{
string[] words = target.Split(' ');
for (int i = 0; i < words.Length; i++)
if (words[i].Length > length)
{
var possible = true;
var ord = 1;
do
{
var lengthTmp = length*ord+ord-1;
if (lengthTmp < words[i].Length) words[i] = words[i].Insert(lengthTmp, " ");
else possible = false;
ord++;
} while (possible);
}
return string.Join(" ", words);
}
我希望看到一个按照我描述的那样执行拆分的代码。谢谢。
最佳答案
这是符合您要求的正则表达式解决方案。请记住,如果您决定稍微更改您的要求,这可能不会起作用,这忠实于 well known quote here。 .
using System.Text.RegularExpressions;
string[] samples = {
@"Hellotoevryone<img height=""115"" width=""150"" alt="""" src=""/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg"" />Iamsogladtoseeall.",
"Testing123Hello.World",
@"Test<a href=""http://stackoverflow.com"">StackOverflow</a>",
@"Blah<a href=""http://stackoverflow.com"">StackOverflow</a>Blah<a href=""http://serverfault.com"">ServerFault</a>",
@"Test<a href=""http://serverfault.com"">Server Fault</a>", // has a space, not matched
"Stack Overflow" // has a space, not matched
};
// use these 2 lines if you don't want to use regex comments
//string pattern = @"^((?:\S(?:\<[^>]+\>)?){1,10})+$";
//Regex rx = new Regex(pattern);
// regex comments spanning multiple lines requires use of RegexOptions.IgnorePatternWhitespace
string pattern = @"^( # match line/string start, begin group
(?:\S # match (but don't capture) non-whitespace chars
(?:\<[^>]+\>)? # optionally match (doesn't capture) an html <...> tag
# to match img tags only change to (?:\<img[^>]+\>)?
){1,10} # match upto 10 chars (tags don't count per your example)
)+$ # match at least once, and match end of line/string
";
Regex rx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
foreach (string sample in samples)
{
if (rx.IsMatch(sample))
{
foreach (Match m in rx.Matches(sample))
{
// using group index 1, group 0 is the entire match which I'm not interested in
foreach (Capture c in m.Groups[1].Captures)
{
Console.WriteLine("Capture: {0} -- ({1})", c.Value, c.Value.Length);
}
}
}
else
{
Console.WriteLine("Not a match: {0}", sample);
}
Console.WriteLine();
}
使用上面的示例,这里是输出(括号中的数字 = 字符串长度):
Capture: Hellotoevr -- (10)
Capture: yone<img height="115" width="150" alt="" src="/Content/Edt/image/b49768
75-8dfb-444c-8b32-cc b47b2d81e0.jpg" />Iamsog -- (116)
Capture: ladtoseeal -- (10)
Capture: l. -- (2)
Capture: Testing123 -- (10)
Capture: Hello.Worl -- (10)
Capture: d -- (1)
Capture: Test<a href="http://stackoverflow.com">StackO -- (45)
Capture: verflow</a> -- (11)
Capture: Blah<a href="http://stackoverflow.com">StackO -- (45)
Capture: verflow</a>Bla -- (14)
Capture: h<a href="http://serverfault.com">ServerFau -- (43)
Capture: lt</a> -- (6)
Not a match: Test<a href="http://serverfault.com">Server Fault</a>
Not a match: Stack Overflow
关于c# - 将 html 拆分为单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/845375/
我有一个数组列表: ArrayList allText = new ArrayList(); 其内容是这样的: [Alabama - Montgomery, Alaska - Juneau, Ariz
我有一个 timestamp 格式的开始和结束时间。我想将它们分成多个时间段,例如 1 小时。 $t1 = strtotime('2010-05-06 12:00:00'); $t2 = strtot
我需要将 span10 分成 3 列,但我无法将它们排列起来。我应该在 span10 中添加一个 span12 还是使用 offset 还是??
我有一个时间序列。我想从早上 8 点到第二天早上 7:59 分成 24 小时的区 block 。我知道如何按日期分组,但我尝试过使用 TimeGroupers 和 DateOffsets 处理这个 8
我收到“街道号码邮政编码城市”形式的地址(作为字符串)。我想要做的是将街道和号码与邮政编码和城市分开。通常你可以按空格分割。但有些街道名称中也有空格,例如:“Emile Van Ermengemlaa
我有一个用户列表。其中一些用户处于第一状态,而其他用户处于第二状态。所以我想要的是将这个列表显示为首先,它按排序顺序显示存在 = 1 的用户,然后按排序顺序显示存在 = 2 的用户。这里的排序是根据用
我感觉我搜索了整个网络,但找不到一种方法将不同高度的 div 很好地划分为 3 列,就像 http://www.ing.nl 上那样 headertekst headerteksttesth
Bootstrap 3 按钮下拉菜单出现问题。你可以在这里看到我的两个例子: http://www.bootply.com/W1dLusilMk http://www.bootply.com/GGBv
我在 php 中执行以下操作 foreach($QuestionAsekd as $k => $v){ $grp_name = $v['NAME']; $groupValues[$gr
我找到了一种用pandas解析html的绝妙方法。我的数据格式有点奇怪(见下文)。我想将这些数据拆分为 2 个单独的数据帧。 注意每个单元格如何由,分隔...是否有任何真正有效的方法来分割所有这些单元
HTML 看起来像这样,但我不允许对其进行更改。我只能编写 CSS 将其变成 2 列。 Povezave www.behance.net www.kiberpipa.org www.o
假设我有以下数据框“A” utilization utilization_billable service 1
我需要将 2 个文本框拉伸(stretch)到 100% 的浏览器宽度,以及一个提交按钮。所有三个都应该在一行中,我试图拉伸(stretch)它但它没有发生......有什么想法吗? 代码: .sea
我是一名优秀的程序员,十分优秀!