How do I optionally match the start ^
or end $
of a line in a regular expression.
我如何选择匹配行的开始^或者结束美元在一个正则表达式。
For example:
例如:
/(?<=[\s\^])/
does not match starts with space character or start of line.
/(? < =[\ s \ ^])/不匹配开头空格字符或线的开始。
As requested my problem was in PHP matching the following.
根据请求,我的问题是在PHP中匹配以下内容。
$str = '**bold** **bold** **bold**';
echo preg_replace('/(?<=\s|^)\*\*(.+?)\*\*(?=\s|$)/', '<strong>\\1</strong>', $str);
My edge cases were the bold at the start and end of the string were not being matched. Some edge cases I came across with other variants were matching within strings, matching chains of asterisks, and countless other problems.
我的边缘是粗体在开始和结束的弦没有被匹配。我遇到过的一些边的例子有字符串内的匹配,星号链的匹配,还有无数其他的问题。
echo preg_replace('/(?<=^|\s|\>)[\*]{2,}(?=[^\s\*])(.+?)(?<=[^\s\*])[\*]{2,}(?=\s|\<|$)/', '<strong>\\1</strong>', $str);
3 个解决方案
#1
3
If you insist on matching after a whitespace character or the start of the line use an alternation (not the character class). Assuming your regex flavour supports alternations in lookbehind assertions.
如果您坚持在空格字符或行开始之后进行匹配,请使用替换(而不是字符类)。假设您的regex风味支持在查找断言后进行更改。
/(?<=\s|^)\w+/m
but probably you are looking for a word boundary \b
? They match on the start or end of a word. (Exactly on the change from a word character to a non word character or the other way round)
但是你可能在找一个单词boundary \b?它们在单词的开头或结尾进行匹配。(从一个字字符到一个非字字符或者反过来)
You should give us more information: the language, the input and the expected result (including corner cases).
您应该向我们提供更多的信息:语言、输入和预期结果(包括角落情况)。
#2
0
Just remove the anchors. Without the anchors, the regex will be matched anywhere in the string you are searching.
只是删除锚点。没有锚,regex将被匹配到您正在搜索的字符串中的任何位置。
#3
0
Look at this:
看看这个:
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.regular-expressions.info/anchors.html
http://www.regular-expressions.info/anchors.html
Regular expression - starting and ending with a letter, accepting only letters, numbers and _
正则表达式——以字母开头和结尾,只接受字母、数字和_
cheers!
干杯!
#1
3
If you insist on matching after a whitespace character or the start of the line use an alternation (not the character class). Assuming your regex flavour supports alternations in lookbehind assertions.
如果您坚持在空格字符或行开始之后进行匹配,请使用替换(而不是字符类)。假设您的regex风味支持在查找断言后进行更改。
/(?<=\s|^)\w+/m
but probably you are looking for a word boundary \b
? They match on the start or end of a word. (Exactly on the change from a word character to a non word character or the other way round)
但是你可能在找一个单词boundary \b?它们在单词的开头或结尾进行匹配。(从一个字字符到一个非字字符或者反过来)
You should give us more information: the language, the input and the expected result (including corner cases).
您应该向我们提供更多的信息:语言、输入和预期结果(包括角落情况)。
#2
0
Just remove the anchors. Without the anchors, the regex will be matched anywhere in the string you are searching.
只是删除锚点。没有锚,regex将被匹配到您正在搜索的字符串中的任何位置。
#3
0
Look at this:
看看这个:
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.w3schools.com/jsref/jsref_obj_regexp.asp
http://www.regular-expressions.info/anchors.html
http://www.regular-expressions.info/anchors.html
Regular expression - starting and ending with a letter, accepting only letters, numbers and _
正则表达式——以字母开头和结尾,只接受字母、数字和_
cheers!
干杯!