validate valid tester regulares regular probar online one expresiones ejemplos regex bash grep

regex - tester - valid regular expression



¿Qué significa ''/ K'' en esta expresión regular? (1)

Dado que no todos los sabores de expresiones regulares son compatibles con la apariencia, Perl introdujo el /K En general cuando tienes:

a/Kb

Cuando “b” coincide, /K le dice al motor que finja que el intento de coincidencia comenzó en esta posición.

En su ejemplo, quiere simular que el intento de coincidencia comenzó en lo que aparece después del texto "" access_token ":".

Este ejemplo demostrará mejor el uso de /K :

~$ echo ''hello world'' | grep -oP ''hello /K(world)'' world ~$ echo ''hello world'' | grep -oP ''hello (world)'' hello world

Dado el siguiente script de shell, ¿alguien sería tan amable de explicar el grep -Po regex por favor?

#!/bin/bash # Issue the request for a bearer token, json is returned raw_json=`curl -s -X POST -d "username=name&password=secret&client_id=security-admin-console" http://localhost:8081/auth/realms/master/tokens/grants/access` # Strip away all but the "access_token" field''s value using a Python regular expression bearerToken=`echo $raw_json | grep -Po ''"''"access_token"''"/s*:/s*"/K([^"]*)''` echo "The bearer token is:" echo $bearerToken

Específicamente, estoy interesado en entender las partes de la expresión regular.

grep -Po ''"''"access_token"''"/s*:/s*"/K([^"]*)''`

y como funciona ¿Por qué tantas citas? ¿Para qué es la "K"? Tengo algo de experiencia con grep regex pero esto me confunde.

Esta es la salida real del comando curl y el script de shell (grep) funciona como se desea, devolviendo solo el contenido del valor "access_token".

{ "Señal_acceso": "eyJhbGciOiJSandNoThisIsntRealndmbS1yZWFsbSI6eyJyb2xlcyI6WyJtYW5hZ2UtY2xpZW50cyIsInZpZXctcmVhbG0iLCJtYW5hZ2UtZXZlbnRzIiwidmlldy1ldmVudHMiLCJ2aWV3LWFwcGxpY2F0aW9ucyIsInZpZXctdXNlcnMiLCJ2aWV3LWNsaWVudHMiLCJtYW5hZ2UtdXNlcnMiLCJtYW5hZ2UtYXBwbGljYXRpb25zIiwibWFuYWdlLXJlYWxtIl19LCJtYXN0ZXItcmVhbG0iOnsicm9sZXMiOlsibWFuYWdlLWV2ZW50cyIsIm1hbmFnZS1jbGllbnRzIiwidmlldy1yZWFsbSIsInZpZXctZXZlbnRzIiwidmlldy1hcHBsaWNhdGlvbnMiLCJ2aWV3LXVzZXJzIiwidmlldy1jbGllbnRzIiwibWFuYWdlLXJlYWxtIiwibWFuYWdlLXVzZXJzIiwibWFuYWdlLWFwcGxpY2F0aW9ucyJdfX19.fQmQKn-xatvflHPAaxCfrrVow3ynpw0sREho7__jZo2d0g1SwZV7Lf4C26CcweNLlb3wmKHHo63HRz35qRxJ7BXyiZwHgXokvDJj13yuOb6Sirg9z02n6fwGy8Iog30pUvffnDaVnUWHfVL-h_R4-OZNf-_YUK5RcL2DHt0zUXI", "expires_in": 60, "refresh_expires_in": 1800, "refresh_token": "eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiJlNWFmYTZiOC.WeiJOC1jQ52aKgnW8UN2Lv9rJ_yKZiOhijOYKLN2EEOkYF8rvRZsSKbTPFKTIUvjnwy2A7V_N-GhhJH4C-T7F5__QPNofSXbCNyvATj52jGLxk9V0Afvk-Z5QAWi55PJRTC0qteeMRcO2Frw-0KtKYe9o3UcGICJubxhZHsXBLA "," token_ty pe ":" ": "eyJhbGciOiJSUzI1NiJ9.eyJuYW1lIjoiIiwianRpIjoiMGIyMGI0ODctOTI4OS00YTFhLTgyNmMtM2NiOTg0MDJkMzVkIiwiZXhwIjoxNDQ2ODI4MDU5LCJuYmYiOjAsImlhdCI6MTQ0NjgyNzk5OIwouldhaveToBeNutsUiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhZG1pbiIsImVtYWlsX3ZlcmlmaWVkIjpmYWxzZX0.DmG8Lm4niL1djzNrLsZ2CrsB1ZzUPnR2Nm7IZnrwrmkXsrPxjl6pyXKCWSj6pbk2sgVI8NNFqrGIJmEJ7gkTZWm328VGGpJsmMuJBki0KbqBRKORGQSgkas_34rwzhcTE3Iki8h_YVs2vvNIx_eZSOvIzyEcP3IGHuBoxcR6W3E", "no-antes-política "," id_token portador": 0 "estado de sesión": "62efc05c-1bf5-4f55-b749-5e0eff94155b"}

En caso de que alguien encuentre esta publicación, esto es lo que terminé usando:

if hash jq 2>/dev/null; then # Use the jq command to safely parse json bearerToken=$(echo $raw_json | jq -r ''.access_token'') else # Strip away all but the "access_token" field''s value using a perl regular expression bearerToken=$(echo $raw_json | grep -Po ''"''"access_token"''"/s*:/s*"/K([^"]*)'') fi