I have the following string:
text='2-05-13 15:31:48.253 StockSpy Realtime Stocks Quote[5353:207339] Websocket: symbols: ({change = "0.5250015";changePercent = "1.49701"; dayVolume = 16608364; exchange = NYQ; id = BAC; marketHours = 1; price = "35.595"; priceHint = 4;quoteType = 8; time = 3304904610000})2022-05-13 15:31:48.256 StockSpy Realtime Stocks Quote[5353:207339] Websocket: Streamer Prices Updat2022-05-13 15:31:48.256 StockSpy Realtime Stocks Quote[5353:207343] refreshG2022-05-13 15:31:48.267 StockSpy Realtime Stocks Quote[5353:207339] webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message: CgReREpJFRSv 0YY8MCp3ZdgKgNESkkwCTgBRb2 wz9ItLvjWGWAnvJD2A2022-05-13 15:31:48.267 StockSpy Realtime Stocks Quote[5353:207339] Websocket: symbols: ( { change = "485.2383"; changePercent = "1.529258"; dayVolume = 186178996; exchange = DJI; id = "^DJI"; marketHours = 1; price = "32215.54"; priceHint = 4; quoteType = 9; time = 3304904614000})'
and I want to extract only the following text:
symbols: ( { change = "485.2383"; changePercent = "1.529258"; dayVolume = 186178996; exchange = DJI; id = "^DJI"; marketHours = 1; price = "32215.54"; priceHint = 4; quoteType = 9; time = 3304904614000})
and all the text in that specific format as this appears mutiple times in my dataset.
Therefore, I need a regex pattern that can extract all the data within the parentheses, including the parentheses and the word symbols
. So something that searches for all the words symbols
and finishes at the last closing parentheses and grabs al of these.
I have used regex101 to build a pattern, and the closest I got to was this pattern by using the following:
^[^\()] (^.)\s
The structure of my text is like in the regex101 however, when it's just as a single string as given above, I used:
\(.*?\)
however it still grabs unncessary text.
CodePudding user response:
You can make the pattern a bit more specific:
\bsymbols:\s*\({change\s*=[^{}]*}\)
Explanation
\bsymbols:\s*
Match the wordsymbols
, then:
and optional whitespace chars\({
Match({
change\s*=
Matchchange
, optional whitepace chars and=
[^{}]*
Match optional chars other than{
and}
}\)
Match})
See a regex101 demo
Or only specifying symbols making the pattern a bit less strict:
\bsymbols:\s*\({[^{}]*}\)
CodePudding user response:
You can do it with this regex:
symbols: *\( *{[^}] } *\)
Explanation:
symbols:
: "symbols" word followed by colon*
: any number of spaces\(
: open parenthesis*
: any number of spaces{
: open curly brace[^}]
: any character other than curly brace}
: closed curly brace*
: any number of spaces)
: closed parenthesis
Try it here.