简体   繁体   中英

selecting string that ends with specific string using regex c#

string is "$$$ dkfdjkfj name $$$ kdfjdf post
dkfdjkfj name name

selecting only string that starts with $$$ and ends with name or starts with space and ends with name and should not have any html tags in between.

Above output is: dkfdjkfj

why do you want to use regex?

You can do it with built in string functionality, for example:

if(myString.StartsWith("$$$") && myString.EndsWith(name) && (!myString.Contains("<") && !myString.Contains(">"))) DoSomething();

You might want to create some more advanced html tag logic. My example assumes that a < and a > in the same string is a html tag.

Edit : I misunderstood your question. You can use substring, example:

var str = myString.Substring(myString.IndexOf("$$$")+3, myString.IndexOf("name")-3);

The indexes are +3 since you don't want to include $$$ or name. If you have many valid sub strings in one string, you have to iterate through it and remember the last index of $$$, and search for the next one from that position.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM