微调正则表达式以提取定界符之间的数据
Fine tuning regex to extract data between delimeters
我正在使用 VBA 正则表达式,我只需要提取两个 # 分隔符之间的数据(我只需要删除前后有任何空格的文本,而不是 # 分隔符)。
到目前为止我有这个模式:
^#\s*(.*)\s*#$
输入的数据有几条规则:
1. Always start with #
2. After the first # there may be zero or one spaces
3. After captured text there may be zero or one spaces before end of line
4. After captured text there may be zero or one # before end of line
例如:
# this is a test
#this is a test
# this is a test#
# this is a test #
在所有四种情况下,this is a test
应该是唯一返回的内容。
我不认为该模式在一百万英里之外,但是当我添加结尾 # 符号以及/缺少空格时似乎有困难....
感谢任何帮助。
您可以使用
修复您当前的解决方案
^#\s*(.*?)\s*#?$
见regex demo。两个要点:1) (.*?)
必须是惰性点模式,2) #?
现在有一个 ?
量词来匹配 #
1 次或 0 次。
但是,您也可以使用 Replace
和
删除前导/尾随匹配项
^#\s*|\s*#?$
模式。不要忘记设置正则表达式 .Global = True
.
我正在使用 VBA 正则表达式,我只需要提取两个 # 分隔符之间的数据(我只需要删除前后有任何空格的文本,而不是 # 分隔符)。
到目前为止我有这个模式:
^#\s*(.*)\s*#$
输入的数据有几条规则:
1. Always start with #
2. After the first # there may be zero or one spaces
3. After captured text there may be zero or one spaces before end of line
4. After captured text there may be zero or one # before end of line
例如:
# this is a test
#this is a test
# this is a test#
# this is a test #
在所有四种情况下,this is a test
应该是唯一返回的内容。
我不认为该模式在一百万英里之外,但是当我添加结尾 # 符号以及/缺少空格时似乎有困难....
感谢任何帮助。
您可以使用
修复您当前的解决方案^#\s*(.*?)\s*#?$
见regex demo。两个要点:1) (.*?)
必须是惰性点模式,2) #?
现在有一个 ?
量词来匹配 #
1 次或 0 次。
但是,您也可以使用 Replace
和
^#\s*|\s*#?$
模式。不要忘记设置正则表达式 .Global = True
.