Extracting Key:Value pairs assoc with regex from string on Javascript
You can use a simple regex for key:value
and use a look using exec
:
var str = 'server:all, nit:4545, search:dql has map';var re = /([\w-]+):([^,]+)/g;
var m;var map = {};
while ((m = re.exec(str)) != null) { map[m[1]] = m[2];}
console.log(map);
PHP Regex extract key-value comma separated
Well, if you can add a comma to the end of the string, I think this works:
(\w+):([^:]+),
Edit:
Jonathan Kuhn is totally right:
(\w+):([^:]+)(?:,|$)
This works
Key Value Match Group Regex where Delimiters can also be contained in quote-enclosed values
How about this?
([\w_]+)=\"(.+?)\"|([\w:\- \.]+)
([\w_]+)
retrieves keys.\"(.+?)\"
retrieves values enclosed by"
.[\w:\-\s\.]+
retrieves values except for values enclosed by"
\s
is included not only space, but also newline character. So I usedinstead of
\s
.
https://regex101.com/r/4jsmYp/2
If you want to separate each line for your data, you can also use ([\w_]+)=\"(.+?)\"|[\w:\-\s\.\[\]>]+
.
Regular expression for parsing name value pairs
No escape:
/([^=,]*)=("[^"]*"|[^,"]*)/
Double quote escape for both key and value:
/((?:"[^"]*"|[^=,])*)=((?:"[^"]*"|[^=,])*)/
key=value,"key with "" in it"="value with "" in it",key=value" "with" "spacesBackslash string escape:
/([^=,]*)=("(?:\\.|[^"\\]+)*"|[^,"]*)/
key=value,key="value",key="val\"ue"Full backslash escape:
/((?:\\.|[^=,]+)*)=("(?:\\.|[^"\\]+)*"|(?:\\.|[^,"\\]+)*)/
key=value,key="value",key="val\"ue",ke\,y=val\,ue
Edit: Added escaping alternatives.
Edit2: Added another escaping alternative.
You would have to clean up the keys/values by removing any escape-characters and surrounding quotes.
How to Parse Repeated Name-Value Pairs using Regex in C#
You can use a simpler pattern to match all pattern occurrences in the string using Regex.Matches
- all the necessary strings will already be grouped:
var text = "{token1:param1}stuff{token2}more stuff{token3:param3}";
var pattern = @"\{(?<key>[^:}]*)(?::(?<value>[^}]*))?}";
var matches = Regex.Matches(text, pattern);
foreach (Match m in matches)
{
Console.WriteLine(m.Value);
Console.WriteLine(m.Groups["key"].Value);
Console.WriteLine(m.Groups["value"].Value);
}
See the C# demo and the regex demo. \{(?<key>[^:}]*)(?::(?<value>[^}]*))?}
matches
\{
- a{
char(?<key>[^:}]*)
- Group "key": zero or more chars other than:
and}
(?::(?<value>[^}]*))?
- an optional non-capturing group matching:
and then Group "value" matching zero or more chars other than}
}
- a}
char.
How to search a string of key/value pairs in Java
Use String.split:
String[] kvPairs = "key1=value1;key2=value2;key3=value3".split(";");
This will give you an array kvPairs
that contains these elements:
key1=value1
key2=value2
key3=value3
Iterate over these and split them, too:
for(String kvPair: kvPairs) {
String[] kv = kvPair.split("=");
String key = kv[0];
String value = kv[1];
// Now do with key whatever you want with key and value...
if(key.equals("specialkey")) {
// Do something with value if the key is "specialvalue"...
}
}
Extracting values from keys inside multiple key-value pair arrays with dart/flutter
You could simply iterate into messages
:
var messages = [
{
'id': 1,
'title': "hello",
'global': true,
},
{
'id': 2,
'title': "bye",
'global': false,
},
{
'id': 3,
'title': "hi",
'global': true,
},
];
messages.forEach((element) {
element["global"] == true ? print(element["title"]) : null;
});
Output:
hello
hi
Extract parameter value from url using regular expressions
You almost had it, just need to escape special regex chars:
regex = /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/;
url = 'http://www.youtube.com/watch?v=Ahg6qcgoay4';
id = url.match(regex)[1]; // id = 'Ahg6qcgoay4'
Edit: Fix for regex by soupagain.
Split a pipe delimited key-value pair separated by '=' symbol
The first one sounds good:
var str = "|User=0101|Name=ImNewUser|IsAdmin=0|RefId=23ae2123cd223bf235|";
var result = {};
str.split('|').forEach(function(x){
var arr = x.split('=');
arr[1] && (result[arr[0]] = arr[1]);
});
Related Topics
Node Server Running But Localhost Refusing to Connect
Javascript Ajax Not Working on ASP.NET C# and Returns a 404
Map, and Removing Commas from an Array in JavaScript
Click on Element Containing Text in Puppeteer
Text Input Allow Only Integer Input in Angularjs
Button Not Working on Mobile Devices But Works on Pc Bootstrap
Show 2 Items Per Row[React Native]
Laravel Reactjs:Changes Are Not Reflecting After Change in My Js(Reactjs) File
How to Convert Seconds to Minutes and Hours in JavaScript
How to Execute a JavaScript Function from the Java Part of Jsp
Javascript/Dom: How to Remove All Event Listeners of a Dom Object
What Is the Opposite of Evt.Preventdefault();
Modify Classname When Element Is Clicked in React
How to Return Blank String If Object Is Undefined in Angularjs Forms
Single Quotes in String With Jquery Ajax