2

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 and I want all options mentioned (e.g. --option).

I'm working with Sublime Text and according to its cheatsheet they support \A and \Z to denote start and end of strings.

Thus, I thought \A--.* \Z should match all strings, starting with -- and ending with .

However, this doesn't match anything at all.

What regex would match all strings, starting with a double dash and ending with a space character? Any occurrence, independent of its position should be matched.

6
  • 1
    What is a string for you? The whole document, a single line, a series of characters that are separated by space...? Commented Apr 11, 2017 at 8:40
  • In the classic sense of the word word, anything separated by a space, is a word and considered a string for me. Commented Apr 11, 2017 at 8:44
  • 2
    @SaAtomic that's a word, but not a string in the sense of this documentation. A string here is the whole series of characters passed to the regex engine, so in this case the whole document. See wiki. So in your question you are looking for a word (not string) starting with -- and ending with a space. Commented Apr 11, 2017 at 8:49
  • 1
    @Wiktor I voted to reopen, as your dupe is definitely not answering the OPs question (which is a bit confusing due to the use of the word string). Commented Apr 11, 2017 at 8:50
  • 1
    @SebastianProske: What a "wording"... Go ahead, answer well :) Commented Apr 11, 2017 at 8:53

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It certainly appears so, I'm sorry about that. Thank you for the clarification and the suggested regex. However, neither of the regex's appear to be working for me. The text I'm trying to apply the regex on is this community.openvpn.net/openvpn/wiki/Openvpn24ManPage
@SaAtomic Seems the Boost regex engine is a bit stricter on the lookbehind size, so I have changed that part a bit. You should have gotten an invalid regex error with the previous attempts? I can't test in sublime at the moment (don't have it installed here), but is working in Notepad++, which uses the same regex engine.
@SebastianProske: Sublime Text 3 uses PCRE, not Boost. (*SKIP)(*F) works fine there.
If it does then (?<!\S)--\w++(?!\S) will work, too. Looks tidier, works faster.
1
foo.*bar

here you will match everything beginning by foo and ending by bar

then in your case try

"--.* "

just tested it in sublime text 3 it works

1 Comment

This doesn't work as expected if there are multiple instances of this on the same line. It will then also select everything between the multiple instances.
0

One thing you can do is add these to .prototype and create your own startsWith() and endsWith() functions

this way you can do string.startsWith("starts with this"); and string.endsWith("ends with this"); I'm using substring instead of indexOf because it's quicker without scanning the entire string. Also, if you pass in an empty string to the functions they return false. Reference link Here

    if ( typeof String.prototype.startsWith != 'function' ) {
  String.prototype.startsWith = function( str ) {
    return str.length > 0 && this.substring( 0, str.length ) === str;
  }
};

if ( typeof String.prototype.endsWith != 'function' ) {
  String.prototype.endsWith = function( str ) {
    return str.length > 0 && this.substring( this.length - str.length, this.length ) === str;
  }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.