Regular expression for { } object for - regex
I have a huge file with a lot of stuff that looks like:
skaune.malmo = rsr.path("m 640.4,516.9 3.9,-2.8 23.8,1.2 13.6,-5.2 3,-3.9 17.4,-1.8 1.2,-3.4 9.7,-4.6 0.5,-1.5 -7.3,-10.3 12.5,-4.5 -2.3,-4.1 3.9,-6.1 -3.4,-11.3 -2.7,-4.6 -2.3,-1.1 -6.4,3.4 -7.2,-0.7 -1.2,-6.8 -5.6,1.1 0,-10.6 -6.8,-5.7 0,-1.1 -11.4,-4.1 -4.1,4.1 0,2.3 1.9,1.5 0,3 -1.9,1.1 -1.1,1.6 -7.2,3 -5.3,0.4 -2.7,1.1 -5.2,5.7 -3.4,3.4 -10.2,5.2 -2.7,3.4 -3,4.6 -1.1,4.5 0,6.8 0,5.7 0.7,5 5.6,12.4 3.4,9.8 3.4,3.9").attr({id: 'path4',parent: 'Skanska_kommuner',fill: '#5eacdd','stroke-width': '0','stroke-opacity': '1'}).transform("t-40.677966,-76.271186").data('id', 'path4');
skaune.bjuv = rsr.path("m 643.8,137 -6.3,-2.8 -2.3,6.8 0,2.8 4.1,10.8 5,8 4.1,0 -0.7,7.3 -3.9,4 3.9,1.2 5.2,12.4 0,1.2 -1.1,2.7 -10.9,-1.1 -1.6,1.8 2.7,3.4 3,-1.2 1.1,5.7 -1.1,5.7 2.7,2.2 -2.7,-0.6 0,0.6 -0.7,5.7 3.4,1.2 1.1,-3 9.8,7.5 2.3,-3.4 3.8,2.3 7.5,-3 7.9,-4.5 -3.4,-11.8 10.2,5 1.6,-2.8 -6.8,-6.3 4.1,-11.3 -4.5,-5 5,-4.6 5.2,0.5 3.8,-8.4 1.8,-5.2 6.9,0 -2.3,-11.4 -3.4,0.7 -4.6,-3.4 -12.4,-14.7 -5.2,2.2 -14.8,-8.6 -3.8,0.7 -5.3,0 -1.5,0 1.5,3.9 -3.4,0 -5,6.8").attr({id: 'path6',parent: 'Skanska_kommuner',fill: '#5eacdd','stroke-width': '0','stroke-opacity': '1'}).transform("t-40.677966,-76.271186").data('id', 'path6');
skaune.astarp = rsr.path("m 713,149.4 3.9,-5.6 -8,-5 8,-13.2 -8,-8.4 -5.6,-18.1 -10.7,2.3 0,-6.8 -7.5,-9.1 -7.2,3.8 -9.8,0 0,3 -6.8,16.3 -3.8,4.5 -13.7,2.3 0,-3.4 -5.6,0.7 0,3.9 -14.8,3.3 1.2,5.7 -1.2,1.8 -1.8,2.8 1.8,4 1.2,2.8 2.7,1.8 7.9,3.4 1.8,-8 6.8,2.8 5,-6.8 3.4,0 -1.5,-3.9 1.5,0 5.3,0 3.8,-0.7 14.8,8.6 5.2,-2.2 12.4,14.7 8.4,6.8 0,-4.1 10.9,0").attr({id: 'path8',parent: 'Skanska_kommuner',fill: '#5eacdd','stroke-width': '0','stroke-opacity': '1'}).transform("t-40.677966,-76.271186").data('id', 'path8');
skaune.orkelljunga = rsr.path("m 753.8,1.6 10.7,-5.7 9.8,-9.1 0,-6.3 18.1,-16.4 15.9,-2.2 13.6,-15.5 19.3,-1.5 7.9,-6.4 12.5,14.8 4.5,-3.4 10.2,5.2 13.6,-3 6.8,-3.8 0.5,2.2 -7.3,1.6 -0.7,9.8 -20.4,10.6 -5,16.6 -4.5,1.6 6.1,25.6 -6.8,2.3 1.8,5.6 -4,2.3 -3.9,-9.1 -7.9,-1.1 -4.6,7.9 -4.5,0.5 0,9.1 -4.5,6.8 -25.7,-2.7 -27.6,32.8 -14.3,-2.2 -3.9,3.8 0,6.4 -6.8,6.1 -8.6,-3.9 1.8,-8.6 -7.9,0.7 -3.4,-7.9 -9.1,-5.7 -2.3,-7.9 11.4,-4.6 -10.9,-5.2 2.9,-3.8 1.2,0 6.8,-1.2 9,-9.1 10.2,-17.6 0,-8.4").attr({id: 'path10',parent: 'Skanska_kommuner',fill: '#5eacdd','stroke-width': '0','stroke-opacity': '1'}).transform("t-40.677966,-76.271186").data('id', 'path10');
and so on... It has a function called .attr({/alot of different stuff i want to delete/}) and I want to replace that with a variable called attr.(style). So everything inside { ... } should be replaced with style. How do I do that? What is the regexp string for { and everything inside }?
Any help appreciated.
You haven't said what kind of regex, but in most types I know you're looking for:
attr\({[^}]+}\)
...and replacing with attr(style) (or attr.(style), if that . after attr wasn't a typo). Depending on regex flavor, you may have to add or remove some backslashes there (for instance, with vim's default magic settings, I believe it would be attr({[^}]\+})). Basically:
Match attr({ literally
Match all characters within the {} using [^}]+
Match }) literally
Related
Matching parameters of a function with Regex
What I have so far: \w+\(((".+")|(\d+))+\)I'm not sure how to go about matching more than one paramter separated by a comma. How can I capture the parameters (and function names) of the following test cases? scroll("foo") scroll("foo",55) scroll("foo","bar") scroll("foo","bar",55) scroll(55) scroll(55,"foo") scroll(55, 13,"foo","bar") For example, in the last one the groups must be scroll, 55, 13, "foo", and "bar". Edit: Language is AS3
Try this lengthy regex: (\"?\w+\"?)\((\"?\w+\"?)(?:[,\s]+(\"?\w+\"?))?(?:[,\s]+(\"?\w+\"?))?(?:[,\s]+(\"?\w+\"?))?\)? The code above is set to capture up to five parameters. You can adjust that by adding/removing this code (?:[,\s]+(\"?\w+\"?))? based on your needs. Demo: https://regex101.com/r/o1RRSG/1
i hope i'm in right way you want to split for example this: scroll ( 55, 13, "foo", "bar" ) to its function name and arguments like this: scroll ( 55, 13, "foo", "bar" ) a better result of expression: i just assume additional white spaces for more accuracy the regex fot would be : [^\t (,)]
Okay, you may not like it, but it's flawless as long as you have ExternalInterface backed with JS available. And it's funny. (While doing this with reg exps is the opposite of fun.) The idea is to let JS do its eval. The manual says you have to pass a function name to ExternalInterface.call(), which is not true: pass just any code that evaluates to a function reference. This way you can inject any JS code into the page where your SWF resides (which is why AllowScriptAccess is such a terribly dangerous attribute). public class Test extends Sprite { public function Test() { var test = "scroll(55, 13,\"foo\",\"bar\", \"now(), we are \\\"screwed\\\")\")"; trace(test); var details = parseFunctionCall(test); trace(details[0]); for (var i = 1; i<details.length; i++) { trace("\t"+i+": "+typeof(details[i])+" "+details[i]); } } private function parseFunctionCall(input:String):Array { if (ExternalInterface.available) { var split:RegExp = /^(\w+)\((.+)\)$/; var info = split.exec(input); var inject = "(function(){return ["+info[2]+"];})"; var params = ExternalInterface.call(inject); params.unshift(info[1]); return params; } return null; } } /* Output: scroll 1: number 55 2: number 13 3: string foo 4: string bar 5: string now(), we are "screwed") */
Regular Expression how to add " to beginning and end of word
I have a json string that looks something like this: { operations: [ validateAddressAndBRClassification, vintageValidateAddressAndGeo, deleteShareInformation, validateCountry, validatePostcode, pageThroughAddress, getFromRelationships, getToRelationships, getUnresolvedAddresses, validateCity, getVisits, getOperationalAddress, getGeo, looseAddressSearch, getServiceInformation, getAddress, getInternalProp, validateAddress, disputeBR, validateAddressAndGeo, supportedCountries, internalMx, getBRProfileHistory, databaseStatus, getBRProfile, addressLookup, getDataSourceMetaInformation, getBRDisputeHistory, validateStateProv, getGeopoliticalElementList, validatePostal, postalLookup, signalDataSourceChange, setInternalProp, getShareInformation, addVisit, addressSearch, getRelatedAddresses, ping, showOperations ] } I need to add a double quote " to the beginning and end of each word. What would the regular expression for this be? So I need it to look like this: { "operations": [ "validateAddressAndBRClassification", "vintageValidateAddressAndGeo", "deleteShareInformation", "etc" }
I'm not sure which programming language you are using but in JavaScript you could achieve it with the following line: string = string.replace(/(\w+)/g, "\"$1\""); Example: https://jsfiddle.net/wLyL0hvk/
How to get all substrings from captured groups in regex in Vala?
I am writing an aplication in Vala that uses regex. What I need to do is wrap all hashtags in string in tags. And I don't quite understand how regex works in Vala. Currently I am trying to do something like this: Regex hashtagRegex = new Regex("(#[\\p{L}0-9_]+)[ #]"); MatchInfo info; if (hashtagRegex.match_all_full(string, -1, 0, 0, out info)) { foreach(string hashTag in info.fetch_all()) string = string.replace(hashTag, "" + hashTag + ""); } but it parses only first hashtag and with the space on the end of it. I am using [ #] at the end of the regex because some users don't separate hashtags with spaces and just write bunch of hashtags like this: #hashtag1#hashtag2#hashtag3 and I want to handle it too. What I need to do is to somehow get an array of all hashtags in string to use it to wrap all of them in tags. How can I do it?
What I need to do is to somehow get an array of all hashtags in string to use it to wrap all of them in tags. How can I do it? No it isn't. Try something like this: private static int main (string[] args) { try { GLib.Regex hashtagRegex = new GLib.Regex ("#([a-zA-Z0-9_\\-]+)"); string res = hashtagRegex.replace_eval ("foo #bar baz #qux qoo", -1, 0, 0, (mi, s) => { s.append_printf ("%s", mi.fetch (1), mi.fetch (0)); return false; }); GLib.message (res); } catch (GLib.Error e) { GLib.error (e.message); } return 0; } I don't know what characters are valid in a hash tag, but you can always tweak the regex as needed. The important part is using a callback to perform the replacement.
Replace expression with subsection using regex?
My IDE PHPstorm allows you to do search and replace using regex, one of the things I find myself often doing is switching the order or action, aka, in function a I will set a value on items from list a using list b as the values. but then in function b I want to invert it. so I want to set a value on items from list b using list a as the values. A proper example is this: var $clipDetailsGame = $('#clipDetailsGame'); var $clipDetailsTitle = $('#clipDetailsTitle'); var $clipDetailsByline = $('#clipDetailsByline'); var $clipDetailsTeamOne = $('#clipDetailsTeamOne'); var $clipDetailsTeamTwo = $('#clipDetailsTeamTwo'); var $clipDetailsReferee = $('#clipDetailsReferee'); var $clipDetailsDescription = $('#clipDetailsDescription'); var $clipDetailsCompetition = $('#clipDetailsCompetition'); function a(clip){ clip.data('gameId' , $clipDetailsGame.val()); clip.data('title' , $clipDetailsTitle.val()); clip.data('byline' , $clipDetailsByline.val()); clip.data('team1' , $clipDetailsTeamOne.val()); clip.data('team2' , $clipDetailsTeamTwo.val()); clip.data('refereeId' , $clipDetailsReferee.val()); clip.data('description' , $clipDetailsDescription.val()); clip.data('competitionId', $clipDetailsCompetition.val()); } function b (clip){ $clipDetailsGame .val(clip.data('gameId')); $clipDetailsTitle .val(clip.data('title')); $clipDetailsByline .val(clip.data('byline')); $clipDetailsTeamOne .val(clip.data('team1')); $clipDetailsTeamTwo .val(clip.data('team2')); $clipDetailsReferee .val(clip.data('refereeId')); $clipDetailsDescription.val(clip.data('description')); $clipDetailsCompetition.val(clip.data('competitionId')); } Excluding the formatting (It's just there to make my point clearer), what kind of regex could I use to do the replacement for me?
Basic regex -- nothing fancy or complex at all Search for: (clip\.data\('[a-zA-Z0-9]+')\s*, (\$[a-zA-Z0-9]+\.val\()(\)\);) Replace with: \$2\$1\$3 The only PhpStorm-related thing here is replacement string format -- you have to "escape" $ to have it work (i.e. it has to be \$2 to use 2nd back-trace instead of just $2 or \2 (as used in other engines)). This will transform this: clip.data('gameId' , $clipDetailsGame.val()); clip.data('title' , $clipDetailsTitle.val()); clip.data('byline' , $clipDetailsByline.val()); clip.data('team1' , $clipDetailsTeamOne.val()); clip.data('team2' , $clipDetailsTeamTwo.val()); clip.data('refereeId' , $clipDetailsReferee.val()); clip.data('description' , $clipDetailsDescription.val()); clip.data('competitionId', $clipDetailsCompetition.val()); into this: $clipDetailsGame.val(clip.data('gameId')); $clipDetailsTitle.val(clip.data('title')); $clipDetailsByline.val(clip.data('byline')); $clipDetailsTeamOne.val(clip.data('team1')); $clipDetailsTeamTwo.val(clip.data('team2')); $clipDetailsReferee.val(clip.data('refereeId')); $clipDetailsDescription.val(clip.data('description')); $clipDetailsCompetition.val(clip.data('competitionId')); Useful link: http://www.jetbrains.com/phpstorm/webhelp/regular-expression-syntax-reference.html
Mopping up (not quite the answer to this question, but another way of organizing the code to make search and replace unnecessary): var $details = {}; var fields = [ 'Game', 'Title', 'Byline', 'TeamOne', 'TeamTwo', 'Referee', 'Description', 'Competition' ]; for(field in fields) { $details[field] = $('#clipDetails' + field); } function a(clip) { for(field in fields) { clip.data(field, $details[fields].val()); } } function b(clip) { for(field in fields) { $details[field].val(clip.data(field)); } } Yes, I know that there are tiny naming inconsistencies that means that this isn't working out of the box, such as Game versus gameId. This is an excellent occasion to clean that up too :). If you still want to keep the title case for the ids (such as #clipDetailsGame in stead of #clipDetailsgame), keep it in title case in the fields array and use toLowerCase where you need lower case. By the way, there is an interesting read on what makes DRY a good thing here: https://softwareengineering.stackexchange.com/questions/103233/why-is-dry-important
Json regex deserialization with JSON.Net
I'm trying to read the following example json from a text file into a string using the JSON.Net parsing library. Content of C:\temp\regeLib.json { "Regular Expressions Library": { "SampleRegex":"^(?<FIELD1>\d+)_(?<FIELD2>\d+)_(?<FIELD3>[\w\&-]+)_(?<FIELD4>\w+).txt$" } } Example code to try and parse: Newtonsoft.Json.Converters.RegexConverter rConv = new Newtonsoft.Json.Converters.RegexConverter(); using (StreamReader reader = File.OpenText(libPath)) { string foo = reader.ReadToEnd(); JObject jo = JObject.Parse(foo);//<--ERROR //How to use RegexConverter to parse?? Newtonsoft.Json.JsonTextReader jtr = new Newtonsoft.Json.JsonTextReader(reader); JObject test = rConv.ReadJson(jtr);//<--Not sure what parameters to provide string sampleRegex = test.ToString(); } It seems I need to use the converter, I know the code above is wrong, but I can't find any examples that describe how / if this can be done. Is it possible to read a regular expression token from a text file to a string using JSON.Net? Any help is appreciated. UPDATE: Played with it more and figured out I had to escape the character classes, once I made the correction below I was able to parse to a JObject and use LINQ to query for the regex pattern. Corrected content C:\temp\regeLib.json { "Regular Expressions Library": { "SampleRegex":"^(?<FIELD1>\\d+)_(?<FIELD2>\\d+)_(?<FIELD3>[\\w\\&-]+)_(?<FIELD4>\\w+).txt$" } } Corrected code using (StreamReader reader = File.OpenText(libPath)) { string content = reader.ReadToEnd().Trim(); JObject regexLib = JObject.Parse(content); string sampleRegex = regexLib["Regular Expressions Library"]["SampleRegex"].ToString(); //Which then lets me do the following... Regex rSampleRegex = new Regex(sampleRegex); foreach (string sampleFilePath in Directory.GetFiles(dirSampleFiles, "*")) { filename = Path.GetFileName(sampleFilePath); if (rSampleRegex.IsMatch(filename)) { //Do stuff... } } } Not sure if this is the best approach, but it seems to work for my case.
i don't understand why you have to store such a small regex in a json file, are you going to expand the regex in the future? if so, rather than doing this JObject regexLib = JObject.Parse(content); string sampleRegex = regexLib["Regular Expressions Library"]["SampleRegex"].ToString(); Consider using json2csharp to make classes, at least it's strongly-typed and make it more maintainable. I think a more appropriate json would look like this (assumptions): { "Regular Expressions Library": [ { "SampleRegex": "^(?<FIELD1>\\d+)_(?<FIELD2>\\d+)_(?<FIELD3>[\\w\\&-]+)_(?<FIELD4>\\w+).txt$" }, { "SampleRegex2": "^(?<FIELD1>\\d+)_(?<FIELD2>\\d+)_(?<FIELD3>[\\w\\&-]+)_(?<FIELD4>\\w+).txt$" } ] } It would make more sense this way to store a regex in a "settings" file