简体   繁体   中英

Check for a specific suffix by RegEx and select entire match including suffix

First of all, this is my first question in the community hence please pardon my wrongs Experts! I am learning regex and faced a scenario where I am failing to create answer by myself.

Let's say if there is humongous paragraph, can we first match on the basis of a specific suffix (say '%') and Only then go back and select the desired logic including suffix?

eg part of the text is "abcd efghMNP 0.40 % ijkl mnopSNP -3.20 % xyz". Now in this, if you notice - and I got this much - that there is pattern like

/([MS]NP[\s\d\.-%]+)/

I want to replace "MNP 0.40 %" or "SNP -3.20 %" with blank. replacing part seems easy :) But the problem is with all my learning I am not able to select desired ONLY IF there exists a '%' at the end of match.

The sequence of match I wish to reach at is -- if suffix '%' exists, then match the previous pattern, and if successful then select everything including suffix and replace with empty.

There are several expressions that would do so, for instance this one with added constraints:

[A-Z]{3}\s+[-+]?[0-9.]+\s*%

Test

 const regex = /[AZ]{3}\\s+[-+]?[0-9.]+\\s*%/gm; const str = `abcd efghMNP 0.40 % ijkl mnopSNP -3.20 % xyz "MNP 0.40 %" or "SNP -3.20 %"`; const subst = ``; // The substituted value will be contained in the result variable const result = str.replace(regex, subst); console.log(result); 

Demo 1

Or a simplified version would be:

[A-Z]{3}(.*?)%

Demo 2

You can not go back in the matching if you have encountered a suffix % , but what you can do is to make it part of the pattern so that is has to be matched.

In Javascript you could perform a zero length lookahead assertion (?= making sure that what is on the right contains a pattern or in this case a % but that will not be a real benefit in this case as you want it to be part of the match.

A bit more specific match could be:

[MS]NP\s*-?\d+(?:\.\d+)?\s*%
  • [MS]NP Match M or S followed by NP
  • \\s*-? Match 0+ times a whitespace char followed by an optional -
  • \\d+(?:\\.\\d+)? Match 1+ digits followed by an optional part to match a dot and 1+ digits
  • \\s*% Match 0+ whitespace chars followed by matching %

Regex demo

Use regular expressions with non-capturing parentheses , like so:

string = 'example string with an intended nested string to match.';
regexp = /(?:intended)(.*)(?:to match)/;
firstMatch = regexp.exec(string)[1]; // " nested string "

The question mark has several uses in regular expressions, the parentheses question mark colon form (?:more regex) is the non-capturing parentheses .

See MDN for more details of exec() , string.match() , and regular expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM