如何使用“正则表达式”获取字符串和一组物品
我有一组项目想要放入字符串中,但我不知道顺序或正确的格式,并且我让“用户”定义此配置,然后我想解析此表达式并返回一个字符串给定一组对象。
示例...
给定元素集 [a1, a2, a3, a4] 和一些表达式
"Printing items: (item)[0] (, item)[2,n-1] and (item)[n]"
产生
"Printing items: a1, a2, a3 and a4"
另一个示例:
"Another way: we have (item)[0] and {n-1} others (</br> item)[2,n-1] </br> and (item)[n]"
结果是
Another way: we have a1 and 3 others
a2
a3
and a4.
我采用了自定义符号,但也许有一些东西可以帮助我,或者可能是标准符号已经为此完成了。有什么想法吗?解决问题的最佳方法是什么?
谢谢。
I have a set of items that I want to put in an string but i don't know the order or the right format, and I am letting the "user" define this configuration, then I want to parse this expression and return an string given a set of objects.
examples...
given the set of elements [a1, a2, a3, a4] and some expressions
"Printing items: (item)[0] (, item)[2,n-1] and (item)[n]"
produces
"Printing items: a1, a2, a3 and a4"
Another example:
"Another way: we have (item)[0] and {n-1} others (</br> item)[2,n-1] </br> and (item)[n]"
results is
Another way: we have a1 and 3 others
a2
a3
and a4.
I have employed a custom notation, but maybe there is something out there that can help me or maybe an standard notation already done for this. Any ideas? What the best way of approaching to a solution.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
典型的字符串解析。
我会说删除对项目的引用,除非您有多个列表。然后包括转义字符。
打印项目:#item#0#、#item#2、%n-1%、"、"# 和 #item#%n%#
另一种方式:我们有 #item#0# 和 %n-1% 其他 # item#2, %n-1%, ""# 和 #item#%n%#
这会更容易解析。
% 之间的任何内容都是计算,# 表示引用。如果引用的是列表,则第二个 # 和第三个 # 包含一个索引。逗号表示范围。第二个逗号表示分隔符。
添加一个哈希表,将用户字符串转换为实际引用。派生用户可以从同一基类引用的所有内容,或者让所有内容实现一个接口。
搜索您的转义字符,如果它是对数组的引用,请查看是否有更多该转义字符,如果您在第三个转义字符之前找到逗号,则您有一个范围,第二个逗号并且您有一个项目分隔符。
然后你可以只标记字符串,而不是解释大括号。
Typical string parsing.
I'd say remove the reference to item unless you have multiple lists. Then include an escape character.
Printing items: #item#0#, #item#2,%n-1%,","# and #item#%n%#
Another way: we have #item#0# and %n-1% others #item#2, %n-1%, ""# and #item#%n%#
This would be much easier to parse.
Anything between % is a calculation, # denotes a reference. If the reference is to a list, the second # and third # enclose a index. Comma indicates a range. Second Comma indicates a separation character.
Add a hash table that converts user strings to actual references. Derive everything the user can reference from the same base class, or have everything implement an interface.
Search for your escape character, if it is a reference to an array, look to see if there is more of that escape character, if you find a comma before the 3rd escape character you have a range, second comma and you have an item separator.
Then you can just tokenize the string, instead of interpreting braces.
听起来您想提供一个用户可定义的格式化机制...如果没有,请忽略此答案的其余部分:)
我可以想到三种可能的解决方案,具体取决于您想要提供的灵活性以及您想要投入的精力1
) 最简单且最不灵活的解决方案是使用 string.Format()。这将允许用户决定显示哪些项目以及以什么顺序显示,但要求用户通过索引而不是名称引用每个值,并且无法处理任意数据集。如果用户尝试引用不存在的值,您还需要处理 FormatException。
2)另一种可能性是以稳定的xml格式生成数据并让用户提供xslt。这最适合少量的大型输出,因为对于大量的简单输出,xslt 可能有点冗长。 System.Xml.Xsl 名称空间似乎提供了一个 Xslt 引擎,但我从未尝试过使用它。
3) 您可以使用.NET 模板引擎。还有另一个关于 .NET 中的模板引擎的问题,但我自己从未使用过其中任何一个。
您能推荐一个 .net 模板引擎吗?
It sounds like you want to provide a user definable formatting mechanism ... if not then disregard the rest of this answer :)
I can think of three possible solutions depending on how flexibility you want to offer and how much effort you want to put in.
1) The simplest and least flexible solution is to use string.Format(). This will allow users to decide what items to show and in what order, but requires the user reference each value by index instead of name and cant handle data sets of arbitrary. Also you would need to handle the FormatException if the user tries to reference a non-existent value.
2) Another possibility is to produce the data in a stable xml format and let the user provide an xslt. This works best for a small number of large output's as the xslt can be a bit long-winded for a large number of simple outputs. The System.Xml.Xsl name space seems to provide an Xslt engine but I have never tried to used it.
3) You could use a .NET template engine. there is another SO question regarding template engines in .NET but I have never used any of them myself.
Can you recommend a .net template engine?
我可能错过了这里所问的内容,但我会执行以下操作:
I may be missing what's being asked here, but I would do the following: