[英]match first space on a line using sublime text and regular expressions
So regular expressions have always been tough for me. 所以正则表达对我来说一直很难。 Im getting frustrated trying to find a regular expression that will select the first white space on a line.
我很沮丧,试图找到一个正则表达式,将选择一行上的第一个空白区域。 So then i can use sublime text to replace that with a /
那么我可以使用sublime文本替换为/
If you could give a quick explanation that would help to 如果你能给出一个有助于的快速解释
In the spirit of @edi's answer, but with some explanation of what's happening. 本着@ edi的回答的精神,但对正在发生的事情有一些解释。 Match the beginning of the line with
^
, then look for a sequence of characters that are not whitespace with [^\\s]*
or \\S*
(the former may work in more editors, libraries, etc than the latter), then find the first whitespace character with \\s
. 将行的开头与
^
匹配,然后使用[^\\s]*
或\\S*
查找不是空格的字符序列(前者可以在比后者更多的编辑器,库等中使用),然后查找带\\s
的第一个空白字符。 Putting these together, you have 把这些放在一起,你有
^[^\s]*\s
You may want to group the non-whitespace and whitespace parts, so you can do the replacement you're talking about: 您可能希望将非空白和空白部分分组,以便您可以进行所讨论的替换:
^([^\s]*)(\s)
Then the replacement pattern is just \\1/
然后替换模式只是
\\1/
你可以使用这个正则表达式。
^([^\s]*)\s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.