You need to echo
the string through a pipe to grep
:
array=($(echo "$matchString" | grep -o 'url={"urlPath":"([^"]+)"'))
Grep reads from a file or standard input. It doesn't accept a string argument to search within.
Also, grep
is going to output the entire match, not the part in parentheses. You probably need to use sed
.
array=($(echo "$matchString" | sed 's/url={"urlPath":"\([^"]\+\).*"/\1/'))
The sed
command works like this:
s///
is the substitute command and its delimiters. You can use another delimiter for convenience if it makes the expression more readable or helps eliminate having to do some escapes. Between the first two delimiters is what we want to change. Between the middle one and the last one is what we want to change it to.
url={"urlPath":"
is just the literal text we are using to help make the match
\( \)
encloses a capture group. What falls bewteen here is what we want to snag.
[^"]
matches any character that's not a double-quote
\+
match one or more of the preceding pattern. So, in this case, that's one or more characters that are not quotation marks.
.*
match zero or more of any character. In this case, it starts at the quote after google.com/
and goes to the end of the string.
\1
outputs what was captured by the first (and only in this case) capture group.
Visually:
url={"urlPath":" http://www.google.com/ ","thisIsOtherText"
-----literal---- -------non-quote------ ---any character---
url={"urlPath":" \( [^"] \) .*