Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

350
Vistas
Regex : Find string between brackets after a string

So far my regex is ->

myRegex = /catch *\(.\) */

I want to be able to search the following string -

try {
print(new Function (astatement)())
} catch (e)  { print(e.stack) }

So that if .stack is present after catch(anything) it should return true, so far I have managed to reach the catch(anything), stuck from here.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Matching for {[^}]+}

Matching simple code in the exception handler, provided there are no nested curly braces:

catch *\((\w+)\) *{[^}]+(\1)(\.stack)[^}]+}

The above regular expression matches these examples
(capturing groups for my sake, remove them if not needed):

try {
  print(new Function (astatement)())
} catch (e)  { print(e.stack) }

try {
  print(new Function (bstatement)())
} catch (exception)  { print(exception.stack) }

You can try it out here:
https://regex101.com/r/PuytDJ/2

Some techniques used here

\w+

Instead of just . I assume you can give the exception instance any name, so maybe more safe to use \w+ here.

{[^}]+

This looks for an opening curly brace and catches every non-closing curly brace until the next expression. It works only if you do not have nested curly braces in your exception handler.

(\1)

Here I reference the exception's variable name found in capturing group 1.

(\.stack)

Here is finding .stack literally

[^}]+}

I continue to parse for all non-curly braces characters and finally the closing curly bracket.

Disclaimer

⚠ This being said, regular expressions cannot parse nested elements very well, or only to a certain degree with increasingly more complex expressions.
So code blocks, HTML/XML Tags, JSON objects etc. are better parsed, not text-searched in.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using a Javascript parser, you can traverse through the syntax-tree (AST) and find each ".stack" inside a catch-block.

For example use slimit in Python:

from slimit import ast
from slimit.parser import Parser
from slimit.visitors import nodevisitor


data = """
try {
    print(new Function(astatement)())
} catch (e) {
    print(e.stack)
}
"""

parser = Parser()
tree = parser.parse(data)

def nextCatch(tree):
    for node in nodevisitor.visit(tree):
        if isinstance(node, ast.Catch):
            return node


def nextIdentifierStack(tree):
    for node in nodevisitor.visit(tree):
        if isinstance(node, ast.Identifier) and node.value == 'stack':
            return node

def printChildren(node):
    for i, ch in enumerate(node.children()):
            print(f"{i:2}: {ch.to_ecma()}")


catch = nextCatch(tree)
printChildren(catch)
stack = nextIdentifierStack(catch)
print(stack.to_ecma())

Prints: the catch block with 2 children and the found stack identifier

 0: e
 1: {
  print(e.stack);
}
stack

See also JavaScript parser in Python

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda