Regular Expression : Match till the ( character or the end of the buffer) but not the character

StackOverflow https://stackoverflow.com/questions/22804767

  •  26-06-2023
  •  | 
  •  

Вопрос

Given the following contents in the smb.conf file. How to remove the [share_11999] section using boost(perl version) regex. The problem is the section [share_11999] can be at the end of the file.

The following regex almost works except that it removes the "[" of the next section.

"[[a-zA-Z0-9_-]+?][^\[]+?path = /store/11999.*?(\'|\[)"

[share_11920]
path = /store/11920
writeable = yes
create mask = 0755

[share_11999]
path = /store/11999
writeable = yes
create mask = 0755

[share_12435]
path = /store/12435
writeable = yes
create mask = 0755

Basically the problem is to match anything until a character except the character or the end of the buffer.

Это было полезно?

Решение

To answer your general question: "How to match anything until a character except the character?"
You can use the following regex:

"(.*)(?=word)"

This uses the "look ahead / look behind" construct (?=something) to match everything until the certain word and excluding that word.

Example

Test string ==> 'I don't understand the meaning of this word or anything else'
"(.*)(?=word)" applied
'I don't understand the meaning of this'

Demo

http://regex101.com/r/nE4dS6

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top