Regex to match a string with specific start/end

后端 未结 3 1064
深忆病人
深忆病人 2021-01-25 09:57

I\'m trying to match all occurrences of strings starting with -- and ending with a single space .

The file I\'m handling is the OpenVPN manual

3条回答
  •  执笔经年
    2021-01-25 10:36

    I think your question caused some confusion due to the use of string. You might want to look up the usage of computer science (e.g. here). What you are looking for is word, starting with -- and ending with a space (or maybe the end of the line).

    You can use (?:^|(?<=\s))--\S+ here.

    • (?:^|(?<=\s)) check that there is a space or the start of a line in front (using a lookbehind)
    • --\S+ match double - and one or more non-space characters
      • note that this will always end at with a space or the end of the line

    Another possibility is (?:^|(?<=\s))--\w+(?=\s|$). Here it looks for a sequence of word characters (letters, digits, underscore) and by a lookahead ensures that it ends with a space or the end of the line.

提交回复
热议问题