1

I have a file which will contain contents similar to the following example:

?[A7]DA<DA-SG
'[G7]G%SD\$DF
#[27]F:./4FFF
?[P9]W3_2SS_F
'[90]GA\\WTER

Each line ends in \r\n.

From this particular file, I need to replace the F:./4FFF part of the line #[27]F:./4FFF.

So far to start, I have this pattern in order to try and capture the part I need to replace:

\#\[27\]([\w\W]*)\r\n

The problem is that between the closing ] and the \r\n, could be any alphanumeric character or symbol.

I think the problem lies in the capturing group??? What is the correct pattern for this; I will be doing this in VBA.

GSerg
  • 76,472
  • 17
  • 159
  • 346
cosmarchy
  • 686
  • 3
  • 9
  • 21
  • Try `\#\[27\]([\w\W]*?)\r\n`. You don’t want `[\w\W]` to match the next `\r\n`, so match as few characters as possible. – Sebastian Simon Aug 01 '19 at 20:39
  • 2
    Is there any pattern to match `F:./4FFF` or would `^(#\[27\]).*`and replace with group 1 also be ok? – The fourth bird Aug 01 '19 at 20:39
  • Possible duplicate of [My regex is matching too much. How do I make it stop?](https://stackoverflow.com/questions/22444/my-regex-is-matching-too-much-how-do-i-make-it-stop) – Sebastian Simon Aug 01 '19 at 20:42

2 Answers2

2

You might be trying to design an expression, that would somewhat look like:

(?<=^#\[\d{2}\])\S*

DEMO 1

Or maybe just:

^(#\[\d+\])\S*

DEMO 2

Emma
  • 27,428
  • 11
  • 44
  • 69
1

Use the multiline option with these (regex.MULTILINE or something)

Two ways to do it

^#\[27\](.*)

or

^#\[27\]([^\r\n]*)

The thing is that \r\n is not needed to stop the match on the line.
It goes to the end without matching them.
This is advantageous if the line is the last one in the file.