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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String pattern search

Status
Not open for further replies.

deejayAr

Technical User
Mar 20, 2008
126
US
Hi I have string like
insert into FaultEvent select id as object_id,
${eventType} as event_type_id, current_timestamp as
timestamp from Ahu((returnAirDewPointTemperature >
(returnAirDewPointTemperatureSetpoint + ${upperDeadband}))
or (returnAirDewPointTemperature <
(returnAirDewPointTemperatureSetpoint - ${lowerDeadband})))
as A where Lib.secondsSinceLastFault(A.id,${eventType}) >= ${snooze}

and there are more

How do I develope the pattern to get value upperDeadband lowerDeadband snooze

mean every value inside of ${value} thanks,
 
Code:
const string TestString = "insert into FaultEvent select id as object_id, ${eventType} as event_type_id, current_timestamp as  timestamp from Ahu((returnAirDewPointTemperature > (returnAirDewPointTemperatureSetpoint + ${upperDeadband})) or (returnAirDewPointTemperature <  (returnAirDewPointTemperatureSetpoint - ${lowerDeadband}))) as A where Lib.secondsSinceLastFault(A.id,${eventType}) >= ${snooze}";
        private void button1_Click(object sender, EventArgs e)
        {
            string pointUp = "point + ${";
            string pointUpEnd = "})) or (retur";
            string pointDown = "point - ${";
            string pointDownEnd = "}))) as A";

            label1.Text = TestString.Substring(
                TestString.IndexOf(pointUp)+pointUp.Length, 
                TestString.IndexOf(pointUpEnd)-(TestString.IndexOf(pointUp)+pointUp.Length));
            label2.Text = TestString.Substring(
                TestString.IndexOf(pointDown)+pointDown.Length, 
                TestString.IndexOf(pointDownEnd)-(TestString.IndexOf(pointDown)+pointDown.Length));
        }

HTH,
Lodlaiden

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this "[red]there was an error[/red]" [black]crap[/black]
 
if there is a fixed set of values then a simple string.Replace will work.
Code:
var text = GetText();
var tokens = new[] 
        {
           new { original = "${value}", @new = "a" },
           new { original = "${eventType}", @new = "b" },
           new { original = "${upperDeadband}", @new = "c" },
           new { original = "${lowerDeadband}", @new = "d" },
        };
Array.ForEach(tokens, t => text = text.Replace(t.original, t.new));
or if you need regex
Code:
var regex = new Regex("\$\{\w+\}");
var matches = regex.Maches(text);
foreach (var match in matches)
{
   ... replace match with new value.
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
It appeared as though he was dealing with a case where the variables would have been replaced with actual values. Need a couple samples to validate though...

Lod

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this "[red]there was an error[/red]" [black]crap[/black]
 
It's not fixed set of value I think jason is right I need regex pattern to find in given string
e.g here are 3 examples
1) insert into FaultEvent select id as object_id, ${eventType} as event_type_id, current_timestamp as timestamp from Dh(preCoolingCoilValve > ${valveMaximum}, (preCoolingAirTemperature > (preCoolingAirTemperatureSetpoint + ${deadband}),${filterObject})) as A where Lib.secondsSinceLastFault(A.id,${eventType}) >= ${snooze} or 2) ${othervar} or 3) insert into FaultEvent select a_id as object_id, ${eventType} as event_type_id, current_timestamp as timestamp from Ahu match_recognize (partition by id measures A.id as a_id pattern (A B C) define B as (B.preCoolingCoilValve - A.preCoolingCoilValve > ${swing}), C as (B.preCoolingCoilValve - C.preCoolingCoilValve > ${swing})) I have to get the inside value of ${}
 
and add this value into an arraylist for further work
 
Jason Meckley
your given method for regex
compiler is complaining about escape sequence
how can you fix
 
there are plenty of resources on the web pertaining to regex. you will need to find the right pattern. Regex is not my strong point, so my guess is as good as yours. I figured it would look something like that though.
Code:
@"\$\{\w+\}"
@"\${\w+}"
@"\$({\w+})"
something like that.? it may be the @ that's needed.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
You're lucky I don't have any legit work today.

Code:
        const string TestString = "insert into FaultEvent select id as object_id, ${eventType} as event_type_id, current_timestamp as  timestamp from Ahu((returnAirDewPointTemperature > (returnAirDewPointTemperatureSetpoint + ${upperDeadband})) or (returnAirDewPointTemperature <  (returnAirDewPointTemperatureSetpoint - ${lowerDeadband}))) as A where Lib.secondsSinceLastFault(A.id,${eventType}) >= ${snooze}";

        private void button1_Click(object sender, EventArgs e)
        {
        Regex r = new Regex("(\\$\\{\\w+\\})");
        foreach (Match m in r.Matches(TestString))
            {label1.Text += Environment.NewLine + m.Value;}
        }

Lodlaiden

If [blue]you have problems[/blue], I want [green]source code[/green] AND [green]error messages[/green], none of this "[red]there was an error[/red]" [black]crap[/black]
 
or
Code:
@"(\$\{\w+\})"

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top