Regular Expression Building Help

1

For the string identified below, I needed to get the ABCD value by using a regular expression, I need help to construct this regular expression.

Team(\\'ABCD\\')

    
asked by anonymous 05.01.2018 / 18:54

2 answers

4

The regular expression would be:

\bTeam\(\+\'([\s\S]+?)\+\'\)

The ([\s\S]+?) is the one that generates group 1 and gets the result only from within Team(\'...'\) , whereas \b at the beginning is a meta-character that identifies if it is a word, ie if it finds something like TestTeam() will ignore, only separated by spaces or strings that start with Team() will "match" with regex

If it's JavaScript:

var str = "Team(\'ABCD\')";
var resultados = /\bTeam\(\+\'([\s\S]+?)\+\'\)/g.exec(str);

console.log(resultados[1]);

var str = "Team(\'foo bar baz\')";
var resultados = /\bTeam\(\+\'([\s\S]+?)\+\'\)/g.exec(str);

console.log(resultados[1]);

If it's PHP:

$str = "Team(\'ABCD\')";

if (preg_match("#\bTeam\(\\+\'([\s\S]+?)\\+\'\)#", $str, $resultados)) {
    var_dump($resultados[1]);
}

If it's Python:

import re

str = "Team(\'ABCD\')"
parttern = r'\bTeam\(\+\'([\s\S]+?)\+\'\)'

p = re.compile(parttern)
resultado = p.search(str).group(1)

print(resultado)
    
05.01.2018 / 19:26
0

You can use this expression:

[A-Z]+\b

It will take the sequence of uppercase ( [A-Z] ) from right to left ( +\b ) until it no longer contains a capital letter, that is, Team(\'ABCD\') , will return ABCD .

See in JavaScript:

var string = "Team(\'ABCD\')";
var result = /[A-Z]+\b/.exec(string);
console.log(result[0]);

Because it returns only 1 result, you use the index [0] in result[0] .

    
05.01.2018 / 22:49