json - Extract only first match with Regex in Scala -



json - Extract only first match with Regex in Scala -

i'm trying extract specific string value json-formatted string in scala. however, going used in production environment, i'm concerned efficiency. currently, i'm using bit of code below:

val r = """identifier=\{s: ([\w\.]+),""".r var identifier: string = ""; r.findallin(queryresult tostring).matchdata foreach { m => identifier = m.group(1) }

my concern efficiency. don't need validate json (that's beingness produced aws, i'm assuming it's good, , if it's not, can't alter it), there's no reason go through overhead of parsing out.

that said, can more efficiently regex, or have go downwards level of finding first occurence of 'identifier={s: ', next occurence of ',' after that, , substring between two? trying r.findfirstin can't figure out way extract grouping want that.

or there other super efficient thing i'm not aware of doing?

regex json scala share|improve question asked oct 30 '14 @ 18:24 childofsoong 1,312519 5   since in production environment, utilize json parser please. – nhahtdh oct 30 '14 @ 18:25 2   better utilize well-established parser seek parsing via regex. premature optimization < completeness – m-z oct 30 '14 @ 18:31      how big json string ? identifier=somestring seem string value within json. lot clearer if can share sample json dealing with. – tuxdna oct 30 '14 @ 18:38      the json string quite large, hence why didn't want overhead of parsing entire thing. , when 'production', don't mean stuff goes on consumers - mean we're using test our server interacts db. – childofsoong oct 30 '14 @ 19:50      @nhahtdh should mention don't know if normal json parser parse content response aws dynamodb. formal json specification says string should written "content" s: content – childofsoong oct 30 '14 @ 20:06 add comment |  1 reply 1 active oldest votes up vote 0 down vote accepted

you want first match:

r.findfirstmatchin(queryresult.tostring).map(_.group(1))

(will option[string].)

that said, i'd utilize streaming json parser every time on regex. yes, regex faster, unless really bottleneck, more worried accuracy.

share|improve answer answered oct 30 '14 @ 20:15 rex kerr 120k15216328      neater utilize r.unanchored, s match { case ru(g) => }. click triangle at: scala-lang.org/files/archive/nightly/2.11.x/api/2.11.x/… – som-snytt oct 31 '14 @ 4:54 add comment |  your answer stackexchange.ifusing("editor", function () { stackexchange.using("externaleditor", function () { stackexchange.using("snippets", function () { stackexchange.snippets.init(); }); }); }, "code-snippets"); stackexchange.ready(function() { inittagrenderer("".split(" "), "".split(" ")); stackexchange.using("externaleditor", function() { // have fire editor after snippets, if snippets enabled if (stackexchange.options.snippets.enabled) { stackexchange.using("snippets", function() { createeditor(); }); } else { createeditor(); } }); function createeditor() { prepareeditor({ heartbeattype: 'answer', convertimagestolinks: true, reputationtopostimages: 10, bindnavprevention: true, postfix: "", ondemand: true, discardselector: ".discard-answer" ,immediatelyshowmarkdownhelp:true }); } });   draft saved draft discarded sign or log in stackexchange.ready(function () { stackexchange.helpers.onclickdraftsave('#login-link'); });

sign using google

sign using facebook

sign using email , password

post guest name email stackexchange.ready( function () { stackexchange.openid.initpostlogin('.new-post-login', 'http%3a%2f%2fstackoverflow.com%2fquestions%2f26660598%2fextract-only-first-match-with-regex-in-scala%23new-answer', 'question_page'); } ); post guest name email discard

posting answer, agree privacy policy , terms of service.

not reply you're looking for? browse other questions tagged regex json scala or ask own question.

asked

1 year ago

viewed

188 times

active

1 year ago

var ados = ados || {}; ados.run = ados.run || []; ados.run.push(function () { ados_add_placement(22,8277,"adzerk1558878323",[5,17,2221]).setzone(45); }); ;"use strict";var _extends=object.assign||function(n){for(var i,r,t=1;t0&&(n.tags=i.join(";"))),n}function k(n){return n.innerhtml.replace(/\s+$/g,"")}function st(n,t,i,u){var c=r(n);if(c===null)return function(){};var h=null,e=null,l=f(),a=function a(){k(c)?(s(h),u(!1,f()-l)):e=o(a,t)};return e=o(a,t),i&&(h=o(function(){s(e);u(!0,f()-l)},i)),function(){s(e);s(h)}}function ht(){var t="careers1",i="careers3",u=e.hash.indexof("large=1")!==-1||!g()||r("#careersadsdoublehigh"),f=u?1:2,n=[f+"=hireme"];return r("#"+t)&&n.push("5="+t),r("#"+i)&&n.push("6="+i),n.join("&")}function d(n,i){var r=ot(_extends({},nt,{zones:ht()})),u,e;n&&(r.azt=1);i&&(r.lw=i);typeof t.innerwidth=="number"&&(r.bw=t.innerwidth);u=["zones","ip","ac","eng","prov","tags","theme","at","remote","seed","lw","azt","sysadmin","bw","nocpm","jobview","cpview"];e=l+"?"+ft(r,u);c=f();p(e)}function ct(n){function h(){u.foreach(w);e.foreach(function(n){lt(n,i[n],s,o)});typeof t.clc_after_load=="function"&&t.clc_after_load()}var i=n.cr,r=n.h,u=n.st,e=object.keys(i),o=c?f()-c:0,s="//"+r+"/ct";h()}function lt(n,t,i,f){var h=t.cl,c=t.cn,o=t.an,l=t.utm,s=(h||[]).join(" "),e=r("#"+n);e&&(s&&(e.classname+=" "+s),e.innerhtml=c.replace("&pt=0","&pt="+(f||0)),e.onmousedown=function(n){for(var t=n.target,r,f,s;t.tagname!=="a"&&t!==e;)t=t.parentnode;t!==e&&(r=[],o&&r.push("an="+o),f=[].filter.call(t.attributes,function(n){return/^data-/.test(n.name)}),f.length>0&&f.foreach(function(n){var t=n.name.replace(/^data-/,"");r.push(u(t)+"="+u(n.value))}),r.push("utm="+u(rt+l)),s="",r.length>0&&(s="?"+r.join("&")),t.href=i+s)})}function g(){return y(h).length>0}function at(n){var t=i.createelement("a");return t.href=n,t.host}function vt(){var t,n;g()?(n=st(h,20,t,d),o(function(){var u=r(h),t;k(u)||(u.parentnode.removechild(u),typeof n=="function"&&n(),t=i.createelement("img"),t.src="//"+at(l)+"/to.gif",t.style.display="none",i.body.appendchild(t))},2e3)):d()}var l=n.u,a=n.o,nt=a===undefined?{}:a,t=window,e=t.location,u=t.encodeuricomponent,v=t.decodeuricomponent,i=t.document,tt=i.body,it=i.getelementsbytagname("head")[0],o=t.settimeout,s=t.cleartimeout,rt="&utm_source="+e.hostname+"&utm_medium=ad&utm_campaign=",h="#sidebar [id^=adzerk].everyonelovesstackoverflow",c=0;t.clc={init:ct,load:vt,ls:p,as:w,tags:b}}).call(null, {"u":"//clc.stackoverflow.com/p.js"});"use strict";var allowedhosts=["stackoverflow.com","serverfault.com"];(allowedhosts[0]==="*"||allowedhosts.indexof(location.hostname)!==-1)&&window.clc.load(); related 2134 regular look match line doesn't contain word? 1326 regex match open tags except xhtml self-contained tags 0 regex / substring extract matching patterns / groups 1 extract viewstate scala regex 1 scala regex pattern matching 0 how utilize replaceallin in scala regex replacing capturing match 0 regex extraction in scala 0 regex not matching in scala 2 regex: match , tokenize in scala 0 scala regex match , split hot network questions name fact mattress can't evenly rotated repeatedly applying same transformation? prevents reviewers providing biased , negative reviews? how long take refuel big jumbo jet? name (and brand / year) of puzzle? utilize of macro in latex slow downwards compilation process? faculty hiring committees overly polite on "denial" answers martian, radiation shielding print difference in thue-morse sequence argument evaluation? possible work more full-time employer? how break new employer don't drink? seeking texts of prayers associated clock alter heyting algbras arise out of simple topos satisfies ultrafilter principle? part of harm halved/doubled harm resistance/vulnerability? idiom or proverb implies " evidence contradicts claim" can square roots come when not distances? compact way write if(..) statement many equalities why sec duration of 9192631770 periods of radiations? upgrading ram in hp laserjet printer improve performance? meaning of "caps not pop watch crystal" in schematic unexpected bias in distribution of consecutive primes have impact on security of encryption algorithms? okay utilize public method within own class? how handle players create unmotivated characters why utilize tms in nmr spectroscopy more hot questions


Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -