Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complex Regular Expression 2

Status
Not open for further replies.

kurie

Programmer
Jun 4, 2008
170
0
0
ZA
i have a string str = SiteAuditID-Integer-0,ShowSubSection-Boolean-false,ShowSample-Boolean-false,Shorten-Boolean-false,ShowAllQns-Boolean-false,ShowSubSummary-Boolean-false,NullValue-Boolean-false,
this string is divided by "," and then futher dived by "-". what i want to do is to given an input value(dynamically) like ShowSubSummary, i would like to search the whole string for the value ShowSubSummary and if i find a match i would like to replace this substring "ShowSubSummary-Boolean-false" with "ShowSubSummary-Boolean-true(or null)"
So values SiteAuditID,... ShowAllQns,...NullValue are the key values in a substring.
I seem to have a problem with fully comrehending how reg expressions work, can someone help me here.
 
Don't use what you don't understand" - Someone smart.

Why don't just writing a little piece of code to do the job? Will be easier to write, to read, to debug, to maintain, to modify ...

Cheers,
Dian
 
if i dont use regular expressions then i will use arrays which are easy but i will end up with a very large piece of code, i know with a couple of line using reg expressions i will achieve that,
 
Well, I tried. There are a couple of regex experts in this forum but you can start with this

Cheers,
Dian
 
>Don't use what you don't understand
I agree with it. @op, if I show you what it looks like to get it done, it does not mean you don't have to work for the view of understanding why it works from the elements shown in it.
[tt]
var s="SiteAuditID-Integer-0,ShowSubSection-Boolean-false,ShowSample-Boolean-false,Shorten-Boolean-false,ShowAllQns-Boolean-false,ShowSubSummary-Boolean-false,NullValue-Boolean-false";
var s_search="ShowSubSummary";
var rx=new RegExp("(^|,)("+s_search+"-Boolean-)(false)(,|$)","g");
var t=s.replace(rx,"$1$2true(or null)$4");
[/tt]
If the pattern looks a bit heavy, it is because the js-engine does not support (yet) lookbehind.
 
thanks guys for your help, i will definately try to understand before i implement like the(false) part is not always false but i will try to make it a dynamic variable.

i dont think there is a problem in me learning new things, and i thought thats what IT programming is all about,if u dont know something you learn and i thought these forums are meant to assist me in learning- its like distance education. i think ever since is i started programming i have leant a lot from forums than i leant from colledge.

thanks

regards
 
in two lines i have implemented something that was going to require over 15lines with arrays

thnanks
 
Glad you got it working.

But I must say not always less lines means better code. I don't say this is the case, but sometimes readibility and mantainability decrease with number of lines.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top