Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# C# Regex Help? Using an integer to search through regex?

TheUninvited

Active Coder
We have this text:
Code:
<snippet>

<content><![CDATA[

${1:first}, ${2:second} ${3:second}

]]></content>

   <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->

<tabTrigger>2p</tabTrigger>

  <!-- Optional: Set a scope to limit where the snippet will trigger -->

   <!-- <scope>source.python</scope> -->

</snippet>

I want to grab this elements and add them to a list is my goal but only add them in the list if the number exist , the elements could be 4 or 5 or 10 it doesn’t matter…
${1:first}
${2:second}
${3:second}

we use this command of regex to grab them :
Regex myRegex = new Regex(@"`(\${)(.*?)(?=})");

and somehow i want to use a counter like to search through , i want something like this ,but i know is not possible because it doesn’t work exactly like that…so any ideas how can i do that in c#? or in general ?


Code:
int num;
Regex myRegex = new Regex(@"(\${num)(.*?)(?=})");

 
Last edited:
When i hop on a computer I can try to help but am on mobile right now!
alright man no problem basically i have another problem.

So the thing is i used this code to find my value and it does:
Code:
int num = 1;


Regex subMarks = new Regex(@"(\${)" + num + @"(.*?)(?=})");
            Match matchsubMarks = subMarks.Match(subInText);
            
    if(matchsubmarks.Success)       
int++;


using the num as an increment but the value does not updates in the regex even though the num goes to number 2.

It supposed to find
${2:second}

but nope...
 
alright man no problem basically i have another problem.

So the thing is i used this code to find my value and it does:
Code:
int num = 1;

int++;
using the num as an increment but the value does not updates in the regex even though the num goes to number 2.

Well, you are starting with num = 1, and then you are incrementing int++, instead of num++
Shouldn't that say num++ ?
And is the submarks value / matchsubmarks inside a loop so that the incremented number is used?
 

Buy us a coffee!

Back
Top Bottom