Reputation: 21
I am trying to get string using RegEx; here is the string:
window.runParams = {};
window.runParams = {blablabla};
How to get the second string {blablabla}
? I am using REGEX:
(?<=window.runParams = ").*(?=;)
But that gets the first string {}
.
Upvotes: 2
Views: 69
Reputation: 862
try modifying your regex so it only accepts matches with non-empty curly brackets \{.+\}
such as
(?<=window\.runParams = )(\{.+\})(?=;)
...there's probably ways to simplify the regex further, depending on you problem...my guess is you don't need the lookahead/lookbehind, e.g. in the example given \{.+\}
will do just fine (returns {blablabla}
) ....but it really depends on the format and content of your file...also remember braces, dots etc have a special meaning in regexes so you probably would want to escape them
Upvotes: 0
Reputation: 3961
The following pattern captures only curly brackets with word character content:
(?<=window.runParams = ){\w+}(?=;)
and will only capture:
{blablabla}
when run against the text:
window.runParams = {};
window.runParams = {blablabla};
See results here:
https://regex101.com/r/mTwA64/1
Upvotes: 0
Reputation: 417
If you want to get string with braces eg: {blablabla}
window.runParams = ({\w+})
If you want to get only the string inside braces eg: blablabla
window.runParams = {(\w+)}
Value of group 1 is your result
Upvotes: 1