1

How use regular expression to match in text passphrase between Passphrase= string and \n char (Select: testpasssword)? The password can contain any characters.

My partial solution: Passphrase.*(?=\\nName) => Passphrase=testpasssword

[wifi_d0b5c2bc1d37_7078706c617967726f756e64_managed_psk]\nPassphrase=testpasssword\nName=pxplayground\nSSID=9079706c697967726f759e69\nFrequency=2462\nFavorite=true\nAutoConnect=true\nModified=2018-06-18T09:06:26.425176Z\nIPv4.method=dhcp\nIPv4.DHCP.LastAddress=0.0.0.0\nIPv6.method=auto\nIPv6.privacy=disabled\n
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
kluszon
  • 375
  • 5
  • 19
  • Capture it with [`Passphrase=(.+)`](https://regex101.com/r/PAFT85/1). – Wiktor Stribiżew Jun 18 '18 at 12:44
  • Do you have it literally or it is a LF? – revo Jun 18 '18 at 12:45
  • This is my case: https://regex101.com/r/AmT6PH/1/. @Wiktor Stribiżew doesn't work in my case. – kluszon Jun 18 '18 at 12:56
  • Are you sure you are testing against the right *literal* string? Show the code. Are you sure you posted the *literal* string here, and not the *string literal*? See [`Passphrase=(.+?)\\n`](https://regex101.com/r/AmT6PH/2), but before you say "it works" or "it doesn't work" check both solutions in the code, NOT at the online regex tester. – Wiktor Stribiżew Jun 18 '18 at 12:57
  • Try `[^=]*(?=\\nName)`. But I suspect it wouldn't be bulletproof if password could contain a `=`. Your best bet is using a capturing group. – revo Jun 18 '18 at 12:58
  • Also add a language or a tool tag in which you are working with regular expressions. – revo Jun 18 '18 at 13:03
  • @Wiktor Stribiżew Yes, it works! I checked it with my code. Passphrase=(.+?)\\n gives me what I want. I working with it with Qt and QRegularExpression library. – kluszon Jun 18 '18 at 13:53
  • That's my mistake. – kluszon Jun 18 '18 at 13:57
  • With `QRegularExpression`, you may use `QRegularExpression rx(R"(Passphrase=\K.+?(?=\\n))");` and grab the match value (`rx.match(str).captured(0)`). – Wiktor Stribiżew Jun 18 '18 at 13:59
  • I posed [an answer](https://stackoverflow.com/a/50911343/3832970) with 2 approaches. – Wiktor Stribiżew Jun 18 '18 at 14:08

2 Answers2

1

The only thing you were missing is the the lazy quantifier telling your regex to only match as much as necessary and a positive lookbehind. The first one being a simple question mark after the plus, the second one just prefacing the phrase you want to match but not include by inputting ?<=. Check the code example to see it in action.

(?<=Passphrase=).+?(?=\\n)

const regex = /(?<=Passphrase=).+?(?=\\n)/gm;
const str = `[wifi_d0b5c2bc1d37_7078706c617967726f756e64_managed_psk]\\nPassphrase=testpasssword\\nName=pxplayground\\nSSID=9079706c697967726f759e69\\nFrequency=2462\\nFavorite=true\\nAutoConnect=true\\nModified=2018-06-18T09:06:26.425176Z\\nIPv4.method=dhcp\\nIPv4.DHCP.LastAddress=0.0.0.0\\nIPv6.method=auto\\nIPv6.privacy=disabled\\n
`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
Ruben Bohnet
  • 392
  • 1
  • 12
1

With QRegularExpression that supports PCRE regex syntax, you may use

QString str = "your_string";
QRegularExpression rx(R"(Passphrase=\K.+?(?=\\n))");
qDebug() << rx.match(str).captured(0);

See the regex demo

The R"(Passphrase=\K.+?(?=\\n))" is a raw string literal defining a Passphrase=\K.+?(?=\\n) regex pattern. It matches Passphrase= and then drops the matched text with the match reset operator \K and then matches 1 or more chars, as few as possible, up to the first \ char followed with n letter.

You may use a capturing group approach that looks simpler though:

QRegularExpression rx(R"(Passphrase=(.+?)\\n)");
qDebug() << rx.match(str).captured(1);   // Here, grab Group 1 value!

See this regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563