Repository: mne-tools/mne-python

Issue:
Brainvision marker parser fails w/ [] characters
I have a (perhaps poorly chosen) marker in a file I received, 
```ini
Mk9=Comment,Discontinuit? [B] 4156 ms,136544,1,0
```
which at `brainvision.py:269` is being found with 
```python
items = re.findall(r"^Mk\d+=(.*)", mk_txt, re.MULTILINE)
```
but this regex stops completely at the `[` character, not finding the rest of the line nor the markers which follow.

I'm not a regex ninja but if I manage to fix, I'll send a PR.  In the meantime, I will change remove the offending characters in the file and move on.
Brainvision marker parser fails w/ [] characters
I have a (perhaps poorly chosen) marker in a file I received, 
```ini
Mk9=Comment,Discontinuit? [B] 4156 ms,136544,1,0
```
which at `brainvision.py:269` is being found with 
```python
items = re.findall(r"^Mk\d+=(.*)", mk_txt, re.MULTILINE)
```
but this regex stops completely at the `[` character, not finding the rest of the line nor the markers which follow.

I'm not a regex ninja but if I manage to fix, I'll send a PR.  In the meantime, I will change remove the offending characters in the file and move on.


Discussion:
Mmh are you sure this is due to the square brackets? Fiddling around a bit on [regex101](https://regex101.com/r/wubbJb/1) shows that the regex works as expected (at least on that line) ...

Could you share the `.vmrk`?
Yes, `.` matches any character including square brackets.
I think @teonbrooks wrote that code? :)
@sappelhoff indeed. I just realized that a few lines up from there, there's a regex for `r"\[.*\]"` that may be the culprit.
Mmh are you sure this is due to the square brackets? Fiddling around a bit on [regex101](https://regex101.com/r/wubbJb/1) shows that the regex works as expected (at least on that line) ...

Could you share the `.vmrk`?
Yes, `.` matches any character including square brackets.
I think @teonbrooks wrote that code? :)
@sappelhoff indeed. I just realized that a few lines up from there, there's a regex for `r"\[.*\]"` that may be the culprit.

Resolving patch:
diff --git a/mne/io/brainvision/brainvision.py b/mne/io/brainvision/brainvision.py
--- a/mne/io/brainvision/brainvision.py
+++ b/mne/io/brainvision/brainvision.py
@@ -307,7 +307,7 @@ def _read_vmrk_events(fname, event_id=None, trig_shift_by_type=None):
     if not m:
         return np.zeros((0, 3))
     mk_txt = txt[m.end():]
-    m = re.search(r"\[.*\]", mk_txt)
+    m = re.search(r"^\[.*\]$", mk_txt)
     if m:
         mk_txt = mk_txt[:m.start()]
 
@@ -340,6 +340,9 @@ def _read_vmrk_events(fname, event_id=None, trig_shift_by_type=None):
             if len(mdesc) > 0:
                 dropped.append(mdesc)
 
+    # Drop all markers with no description from the dropped markers
+    # For example, the "New Segment" markers will be dropped from the list
+    # of dropped markers because they usually look like: Mk1=New Segment,,1,1,0
     if len(dropped) > 0:
         dropped = list(set(dropped))
         examples = ", ".join(dropped[:5])


Test patch:
diff --git a/mne/io/brainvision/tests/data/testv2.vmrk b/mne/io/brainvision/tests/data/testv2.vmrk
--- a/mne/io/brainvision/tests/data/testv2.vmrk
+++ b/mne/io/brainvision/tests/data/testv2.vmrk
@@ -17,12 +17,13 @@ Mk3=Stimulus,S255,497,1,0
 Mk4=Stimulus,S254,1770,1,0
 Mk5=Stimulus,S255,1780,1,0
 Mk6=Stimulus,S254,3253,1,0
-Mk7=Stimulus,S255,3263,1,0
-Mk8=Stimulus,S253,4936,1,0
-Mk9=Stimulus,S255,4946,1,0
-Mk10=Response,R255,6000,1,0
-Mk11=Stimulus,S254,6620,1,0
-Mk12=Stimulus,S255,6630,1,0
+Mk7=Comment,comment using [square] brackets,3254,1,0
+Mk8=Stimulus,S255,3263,1,0
+Mk9=Stimulus,S253,4936,1,0
+Mk10=Stimulus,S255,4946,1,0
+Mk11=Response,R255,6000,1,0
+Mk12=Stimulus,S254,6620,1,0
+Mk13=Stimulus,S255,6630,1,0
 
 [Marker User Infos]
 ; Each entry: Prop<Number>=Mk<Marker number>,<Type>,<Name>,<Value>,<Value2>,...,<ValueN>
diff --git a/mne/io/brainvision/tests/test_brainvision.py b/mne/io/brainvision/tests/test_brainvision.py
--- a/mne/io/brainvision/tests/test_brainvision.py
+++ b/mne/io/brainvision/tests/test_brainvision.py
@@ -489,7 +489,6 @@ def test_events():
                                 [7699, 1, 1]])
 
     # Error handling of trig_shift_by_type
-
     pytest.raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
                   preload=True, trig_shift_by_type=1)
     pytest.raises(TypeError, read_raw_brainvision, vhdr_path, eog=eog,
@@ -504,6 +503,20 @@ def test_events():
                       preload=True, trig_shift_by_type={'response': 1000},
                       response_trig_shift=1001)
 
+    # Check that events of type "Comment" are read if they contain square
+    # brackets (which usually signify a new section within a BrainVision file)
+    # If no event_id specified, skip the marker and continue as planned
+    raw = read_raw_brainvision(vhdr_v2_path)
+    events = raw._get_brainvision_events()
+    assert events.shape == (11, 3)  # shape of events without the comment
+
+    # with event_id specified, get that comment and assert it's there
+    tmp_event_id = {'comment using [square] brackets': 999}
+    raw = read_raw_brainvision(vhdr_v2_path, event_id=tmp_event_id)
+    events = raw._get_brainvision_events()
+    assert 999 in events[:, -1]
+    assert events.shape == (12, 3)
+
     # check that events are read properly when event_id is specified for
     # auxiliary events
     with warnings.catch_warnings(record=True):  # dropped events