用文本日期取代数字日期用文本日期取代数字日期用文本日期取代数字日期用文本日期取代数字日期
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
用文本日期取代数字日期
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式范例 » 用文本日期取代数字日期

范例
正则表达式范例
数字范围
浮点数
电子邮件地址
IP 地址
有效的日期
数字日期转文本
信用卡号码
符合完整行
删除重复行
编程
两个相近的字词
陷阱
灾难性的回溯
重复太多次
拒绝服务
让所有东西都可选择
重复的捕获组
混合 Unicode 和 8 比特
此网站的更多信息
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

用文本日期取代数字日期

此范例显示如何将 1/1/50 或 01/01/50 到 12/31/49 的数字日期替换为文本等效日期,从 1950 年 1 月 1 日到 2049 年 12 月 31 日。如果你可以根据匹配的内容改变替换,则只需一个正则表达式就能做到这一点。运行此操作的一种方法是在进程码中创建每个替换。此范例显示如何使用替换字符串条件来运行此操作。此范例可搭配Boost C++ 函数库和PCRE2 C 函数库使用。

若要能够使用替换字符串条件,正则表达式需要为需要不同替换的每一个比对部分创建一个独立的捕获组。每个月份都需要替换成其名称,因此我们需要一个独立的捕获组来比对每个月份号码。以 1、2 和 3 结尾的基数需要有独特的后缀。因此我们需要四个群组来比对以 1、2、3 或其他数字结尾的日期号码。我们假设年份号码 50 至 99 为 1950 至 1999,而年份号码 00 至 49 为 2000 至 2049。因此我们需要再两个群组来比对每个半世纪。

正则表达式

将这一切组合在一起会产生一个相当长的正则表达式。 自由间距 有助于保持其可读性。正则表达式的结构与您用于 比对有效日期 的结构相同。它只会更冗长,因为我们需要 12 个 选项 来比对月份、4 个选项来比对日期,以及 2 个选项来比对年份。

\b
(?: # Month
   
(?'jan'0?1)|(?'feb'0?2)|(?'mar'0?3)|(?'apr'0?4)|(?'may'0?5)|(?'jun'0?6)
  
|(?'jul'0?7)|(?'aug'0?8)|(?'sep'0?9)|(?'oct'10)|(?'nov'11)|(?'dec'12)
  
) /
0?(?: # Day
   
(?'1st'[23]?1)|(?'2nd'2?2)|(?'3rd'2?3)|(?'nth'30|1[123]|[12]?[4-90])
  
) /
(?: # Year
   
(?'19xx'[5-9][0-9])|(?'20xx'[0-4][0-9])
  
)
\b

替换字符串

替换字符串将使用反向引用来重新插入日期号码。由于我们希望从替换中省略前导零,因此我们将 0? 放在日期号码的捕获组之外。这表示我们的正则表达式也允许 10 至 31 日的前导零。由于我们的目标是替换日期,而不是验证它们,因此我们可以接受这一点。否则,我们将需要两组四个选项来比对月份中的日期。一组用于个位数日期,一组用于两位数日期。

很遗憾,自由间距不适用于替换字符串。因此替换包含一行非常长的字符串。它在此处分成多行以符合页面宽度。这是使用 Boost 语法的替换

(?{jan}一月)(?{feb}二月)(?{mar}三月)(?{apr}四月)(?{may}五月)(?{jun}六月)
(?{jul}七月)(?{aug}八月)(?{sep}九月)(?{oct}十月)(?{nov}十一月)
(?{dec}十二月) (?{1st}${1st}日)(?{2nd}${2nd}日)(?{3rd}${3rd}日)(?{nth}${nth}日)
, (?{19xx}19${19xx})(?{20xx}20${20xx})

这是使用 PCRE2 语法的替换

${jan:+一月}${feb:+二月}${mar:+三月}${apr:+四月}${may:+五月}${jun:+六月}
${jul:+七月}${aug:+八月}${sep:+九月}${oct:+十月}${nov:+十一月}
${dec:+十二月} ${1st:+${1st}日}${2nd:+${2nd}日}${3rd:+${3rd}日}${nth:+${nth}日}
, ${19xx:+19${19xx}}${20xx:+20${20xx}}

首先,我们有 12 个条件式,参考 12 个用于月份的捕获组。每个条件式会在群组参与时插入月份名称。当群组未参与时,它们不会插入任何内容。由于在任何比对中只会有一个群组参与,因此只会有一个条件式实际插入任何内容到替换中。

接着,我们有一个文本空格和另外 4 个条件式,参考 4 个用于日期的捕获组。当群组参与时,条件式会使用 反向引用 到同一个群组,以重新插入群组比对到的日期数字。反向引用后接一个文本后缀。

最后,我们有一个文本逗号、一个文本空格和另外 2 个条件式,用于年份。条件式再次使用文本和反向引用,将年份从 2 位数扩充为 4 位数。

用文字日期取代數字日期
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式範例 » 用文字日期取代數字日期

範例
正規表示式範例
數字範圍
浮點數
電子郵件地址
IP 位址
有效的日期
數字日期轉文字
信用卡號碼
符合完整行
刪除重複行
程式設計
兩個相近的字詞
陷阱
災難性的回溯
重複太多次
拒絕服務
讓所有東西都可選擇
重複的擷取群組
混合 Unicode 和 8 位元
此網站的更多資訊
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

用文字日期取代數字日期

此範例顯示如何將 1/1/50 或 01/01/50 到 12/31/49 的數字日期替換為文字等效日期,從 1950 年 1 月 1 日到 2049 年 12 月 31 日。如果你可以根據匹配的內容改變替換,則只需一個正規表示式就能做到這一點。執行此操作的一種方法是在程序碼中建立每個替換。此範例顯示如何使用替換字串條件來執行此操作。此範例可搭配Boost C++ 函式庫和PCRE2 C 函式庫使用。

若要能夠使用替換字串條件,正規表示式需要為需要不同替換的每一個比對部分建立一個獨立的擷取群組。每個月份都需要替換成其名稱,因此我們需要一個獨立的擷取群組來比對每個月份號碼。以 1、2 和 3 結尾的基數需要有獨特的字尾。因此我們需要四個群組來比對以 1、2、3 或其他數字結尾的日期號碼。我們假設年份號碼 50 至 99 為 1950 至 1999,而年份號碼 00 至 49 為 2000 至 2049。因此我們需要再兩個群組來比對每個半世紀。

正規表示式

將這一切組合在一起會產生一個相當長的正規表示式。 自由間距 有助於保持其可讀性。正規表示式的結構與您用於 比對有效日期 的結構相同。它只會更冗長,因為我們需要 12 個 選項 來比對月份、4 個選項來比對日期,以及 2 個選項來比對年份。

\b
(?: # Month
   
(?'jan'0?1)|(?'feb'0?2)|(?'mar'0?3)|(?'apr'0?4)|(?'may'0?5)|(?'jun'0?6)
  
|(?'jul'0?7)|(?'aug'0?8)|(?'sep'0?9)|(?'oct'10)|(?'nov'11)|(?'dec'12)
  
) /
0?(?: # Day
   
(?'1st'[23]?1)|(?'2nd'2?2)|(?'3rd'2?3)|(?'nth'30|1[123]|[12]?[4-90])
  
) /
(?: # Year
   
(?'19xx'[5-9][0-9])|(?'20xx'[0-4][0-9])
  
)
\b

替換字串

替換字串將使用反向參照來重新插入日期號碼。由於我們希望從替換中省略前導零,因此我們將 0? 放在日期號碼的擷取群組之外。這表示我們的正規表示式也允許 10 至 31 日的前導零。由於我們的目標是替換日期,而不是驗證它們,因此我們可以接受這一點。否則,我們將需要兩組四個選項來比對月份中的日期。一組用於個位數日期,一組用於兩位數日期。

很遺憾,自由間距不適用於替換字串。因此替換包含一行非常長的字串。它在此處分成多行以符合頁面寬度。這是使用 Boost 語法的替換

(?{jan}一月)(?{feb}二月)(?{mar}三月)(?{apr}四月)(?{may}五月)(?{jun}六月)
(?{jul}七月)(?{aug}八月)(?{sep}九月)(?{oct}十月)(?{nov}十一月)
(?{dec}十二月) (?{1st}${1st}日)(?{2nd}${2nd}日)(?{3rd}${3rd}日)(?{nth}${nth}日)
, (?{19xx}19${19xx})(?{20xx}20${20xx})

這是使用 PCRE2 語法的替換

${jan:+一月}${feb:+二月}${mar:+三月}${apr:+四月}${may:+五月}${jun:+六月}
${jul:+七月}${aug:+八月}${sep:+九月}${oct:+十月}${nov:+十一月}
${dec:+十二月} ${1st:+${1st}日}${2nd:+${2nd}日}${3rd:+${3rd}日}${nth:+${nth}日}
, ${19xx:+19${19xx}}${20xx:+20${20xx}}

首先,我們有 12 個條件式,參考 12 個用於月份的擷取群組。每個條件式會在群組參與時插入月份名稱。當群組未參與時,它們不會插入任何內容。由於在任何比對中只會有一個群組參與,因此只會有一個條件式實際插入任何內容到替換中。

接著,我們有一個文字空格和另外 4 個條件式,參考 4 個用於日期的擷取群組。當群組參與時,條件式會使用 反向參照 到同一個群組,以重新插入群組比對到的日期數字。反向參照後接一個文字字尾。

最後,我們有一個文字逗號、一個文字空格和另外 2 個條件式,用於年份。條件式再次使用文字和反向參照,將年份從 2 位數擴充為 4 位數。

Replacing Numerical Dates with Textual Dates
  • 简
  • 繁
  • En
About Regular Expressions » Sample Regular Expressions » Replacing Numerical Dates with Textual Dates

Examples
Regular Expressions Examples
Numeric Ranges
Floating Point Numbers
Email Addresses
IP Addresses
Valid Dates
Numeric Dates to Text
Credit Card Numbers
Matching Complete Lines
Deleting Duplicate Lines
Programming
Two Near Words
Pitfalls
Catastrophic Backtracking
Too Many Repetitions
Denial of Service
Making Everything Optional
Repeated Capturing Group
Mixing Unicode & 8-bit
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

Replacing Numerical Dates with Textual Dates

This example shows how you can replace numerical dates from 1/1/50 or 01/01/50 through 12/31/49 with their textual equivalents from January 1st, 1950 through December 31st, 2049. This is only possible with a single regular expression if you can vary the replacement based on what was matched. One way to do this is to build each replacement in procedural code. This example shows how you can do it using replacement string conditionals. This example works can be used with the Boost C++ library, and the PCRE2 C library.

To be able to use replacement string conditionals, the regular expression needs a separate capturing group for each part of the match that needs a different replacement. Each month needs to be replaced with its own name, so we need a separate capturing group to match each month number. Cardinal numbers ending with 1, 2, and 3 have unique suffixes. So we need four groups to match day numbers ending with 1, 2, 3, or another digit. We’re assuming year numbers 50 to 99 to be 1950 to 1999, and year numbers 00 to 49 to be 2000 to 2049. So we need two more groups to match each half century.

The Regular Expression

Putting this all together results in a rather long regular expression. Free-spacing helps to keep it readable. The structure of the regex is the same as what you would use for matching valid dates. It’s only more long-winded because we need 12 alternatives to match the month, 4 alternatives to match the day, and 2 alternatives to match the year.

\b
(?: # Month
   
(?'jan'0?1)|(?'feb'0?2)|(?'mar'0?3)|(?'apr'0?4)|(?'may'0?5)|(?'jun'0?6)
  
|(?'jul'0?7)|(?'aug'0?8)|(?'sep'0?9)|(?'oct'10)|(?'nov'11)|(?'dec'12)
  
) /
0?(?: # Day
   
(?'1st'[23]?1)|(?'2nd'2?2)|(?'3rd'2?3)|(?'nth'30|1[123]|[12]?[4-90])
  
) /
(?: # Year
   
(?'19xx'[5-9][0-9])|(?'20xx'[0-4][0-9])
  
)
\b

The Replacement String

The replacement string will use backreferences to reinsert the date numbers. Since we want to omit leading zeros from the replacements, we placed 0? outside the capturing groups for date numbers. This means that our regex also allows leading zeros for days 10 to 31. Since our goal is to replace dates rather than validate them, we can live with this. Otherwise, we would need two sets of four alternatives to match the day of the month. One set for single digit days, and one set for double digit days.

Unfortunately, free-spacing does not work with replacement strings. So the replacement consists of one very long line. It is broken into multiple lines here to fit the width of the page. This is the replacement using Boost syntax:

(?{jan}January)(?{feb}February)(?{mar}March)(?{apr}April)(?{may}May)(?{jun}June)
(?{jul}July)(?{aug}August)(?{sep}September)(?{oct}October)(?{nov}November)
(?{dec}December) (?{1st}${1st}st)(?{2nd}${2nd}nd)(?{3rd}${3rd}rd)(?{nth}${nth}th)
, (?{19xx}19${19xx})(?{20xx}20${20xx})

This is the replacement using PCRE2 syntax:

${jan:+January}${feb:+February}${mar:+March}${apr:+April}${may:+May}${jun:+June}
${jul:+July}${aug:+August}${sep:+September}${oct:+October}${nov:+November}
${dec:+December} ${1st:+${1st}st}${2nd:+${2nd}nd}${3rd:+${3rd}rd}${nth:+${nth}th}
, ${19xx:+19${19xx}}${20xx:+20${20xx}}

First we have 12 conditionals that reference the 12 capturing groups for the months. Each conditional inserts the month’s name when their group participates. They insert nothing when their group does not participate. Since only one of these groups participates in any match, only one of these conditionals actually inserts anything into the replacement.

Then we have a literal space and 4 more conditionals that reference the 4 capturing groups for the days. When the group participates, the conditional uses a backreference to the same group to reinsert the day number matched by the group. The backreference is followed by a literal suffix.

Finally, we have a literal comma, a literal space, and 2 more conditionals for the year. The conditionals again use literal text and a backreference to expand the year from 2 to 4 digits.

©2015-2025 艾丽卡 support@alaica.com