指定模式可以出现的次数

737 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第35天,点击查看活动详情

指定模式可以出现的次数

  • 要定义模式可在目标操作数中出现的次数范围,请使用以下格式:
    n.n
	s str = "NNNNN"
	s match = str ? 1.5"N"
	w match,!
	
	s str = "NNN"
	s match = str ? 1.5"N"
	w match,!
	
	s str = "nbnbnbnb"
	s match = str ? 1.5"nb"
	w match,!
  • 匹配出现N次特殊模式代码
	s str = "Io"
 	s match = str ? 1.2A
 	w match,!
  • 省略前导第一个n的默认值为零。省略后导第二个n的默认值是任何数字。都省略匹配任意数量字符。
	s str = ""
 	s match = str ? .2L
 	w match,!
 	
	s str = "ABCDEFG"
 	s match = str ? 1.U
 	w match,!
        
	s str = "aaaaaaaaaaa"
	s match = str ? .A
	w match,!

指定多个模式

  • 要定义多个模式,可以将n和模式组合成任意长度的序列。
	s str = "2021-07-11"
 	s match = str ? 4N1"-"2N1"-"2N
 	w match,!
 	
	s str = "7/11/21"
 	s match = str ? 1.2N1"/"1.2N1"/"1.4N
 	w match,!
 	
	s str = "12/1/2021"
 	s match = str ? 1.2N1"/"1.2N1"/"1.4N
 	w match,!

指定组合模式

Pattern1Pattern2
  • 使用组合模式时,将对照目标操作数检查由pattern1后跟pattern2组成的序列。
	s str = "123"
 	s match = str ? 3N.4L
 	w match,!
 	
	s str = "123abcde"
 	s match = str ? 3N.4L
 	w match,!

指定交替模式

  • 测试操作数是否与一组指定模式序列中的一个或多个匹配。它为模式匹配提供了逻辑或功能。
( pattern-element sequence {, pattern-element sequence }...)
  • 如果str包含字母“A”的一个匹配项或字母“B”的一个匹配项,则以下模式返回TRUE(1)
	s str = "A"
 	s match = str ? 1(1"A",1"B")
 	w match,!
 	
	s str = "B"
 	s match = str ? 1(1"A",1"B")
 	w match,!
 	
	s str = "AB"
 	s match = str ? 1(1"A",1"B")
 	w match,!
 	
  • 重复计数大于一的交替可以产生许多可接受模式的组合。
 	s str = "YX"
 	s match = str ? 2(1"Y",1"X")
 	w match,!
 	
 	s str = "YY"
 	s match = str ? 2(1"Y",1"X")
 	w match,!
 	
 	s str = "XXY"
 	s match = str ? 2(1"Y",2"X")
 	w match,!
 	
 	s str = "XXXX"
 	s match = str ? 2(1"Y",2"X")
 	w match,!
  • 匹配15位与18位身份证号
 	s str = "123456789012345"
 	s match = str ? 15N
 	w match,!
 	
 	s str = "12345678901234567X"
 	s match = str ? 17N1"X"
 	w match,!
 	
 	s str = "123456789012345"
 	s match = str ? 1(17N1"X",15N)
 	w match,!
 	
 	s str = "12345678901234567X"
 	s match = str ? 15N1(2N1"X",0N)
 	w match,!