字符串模板

发布于 2025-01-04 17:25:19 字数 450 浏览 3 评论 0原文

我的任务是设计一个功能,生成遵循可以稍后扩展的规则的 ID 号。

例如;系统可能需要按如下方式生成其 ID 号:18122424。这将分为:[DepartmentId][YearCreated][Sequential]。部门= 18,年份= 12,序列= 2424。

我如何设计一个允许用户更改它的规则引擎?我想出了这样的格式:

  1. Dept(#)
  2. Year(#)
  3. Seq(#)
  4. Initials(#) <-- 姓名缩写。

因此,上述 Id 的规则为:[Dept(2)][Year(2)][Seq(4)]。如果我将其作为字符串获取,如何解析它以获取规则?正则表达式还是普通字符串搜索?

从概念上讲,是否有更简单或更有效的方法来做到这一点?

I have the task of designing a feature that generates Id numbers that follow rules that can be extended later.

For example; A system may require their id numbers be generated as follows: 18122424. This would be broken down into: [DepartmentId][YearCreated][Sequential]. Department = 18, Year = 12, Sequence = 2424.

How do I go about designing a rule engine that allows the user to change it? I came up with a format like:

  1. Dept(#)
  2. Year(#)
  3. Seq(#)
  4. Initials(#) <-- Name initials.

So the rule for that Id above is: [Dept(2)][Year(2)][Seq(4)]. If I get this as a string, how do I parse it to get the rules? Regex or normal string search?

Is there an easier or more efficient way of doing this conceptually?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流心雨 2025-01-11 17:25:19

如果我是对的,您的问题是关于解析规则字符串,即检索字段名称和长度。

我会一次处理一个字段,并可能使用 Regexpr 来表示 [ 字母(数字 ) ],例如

\[{[A-Za-z]+}\({[0-9]+}\)\]

(MSVC 语法,检索两个标记表达式)。

您还需要存储可能的字段名称的字典并将数字转换为整数。

或者,组合 C strchr 和 scanf 也可以实现这一目的。

If I am right, your question is about parsing the rule string, i.e. retrieving the field names and lenghts.

I would work one field at a time and possibly use a Regexpr for [ letters ( digits ) ], like

\[{[A-Za-z]+}\({[0-9]+}\)\]

(MSVC syntax, retrieve the two tagged expressions).

You'll also need to store a dictionary of possible field names and convert the digits to an integer.

Alternatively, combined C strchr and scanf can do the trick.

痴意少年 2025-01-11 17:25:19

这不是“规则引擎”,这只是模板。

无论您使用什么语言,都有某种模板以及指定所有这些格式的方法。就用那个吧。

This is not a "rule engine", this is just templating.

Whatever language you are using has some kind of templating, and a way to specify all of these formats. Just use that.

笑,眼淚并存 2025-01-11 17:25:19

我不知道是谁投的票。

生成数字
- 你首先必须决定模板。类似depid-year-seq。假设你只有这 3 个“变量”。然后你应该将这些变量映射到原始值。使用字符串解析器并在 - 上拆分“给定”模板。它将为您提供索引为 0 = depid、1 = 年、2 = seq 的数组。循环遍历数组并使用给定索引上每个字符串的相应值创建一个字符串,即
- 0 = depid =“18”
- 1 = 年 = “18” + “12” = “1812”
- 2 = seq = "1812" + "1212" = "18121212"

反转
你应该通过将其分成 2-2-rest 来解析你给定的数字.. .. 我想你可以从这里开始。

I don't know who upvoted it.

Generating the number
- you first have to decide the template. something like depid-year-seq. Assuming you have only these 3 "Variables". Then you should have have these variables mapped to original value. use a string parser and split the "given" template on -. It will give you array with index 0 = depid, 1 = year, 2 = seq. Loop through the array and create a string by using the corresponding values for each string on given index i.e.
- 0 = depid = "18"
- 1 = year = "18" + "12" = "1812"
- 2 = seq = "1812" + "1212" = "18121212"

Reverse
you should parse your given number by splillting it up in 2-2-rest .. .. i guess you can pick up from here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文