I have multiple strings that looks:
this is ab-skdn string
ab-sdhnif my string
For each string, I need to pull the part that is ab-**
For example I need ab-skdn and ab-sdhnif
How could I do that using C#
My code would look like something:
var myString = "this is ab-skdn string"
match = "ab-skdn"
Use a regex to find the matches. A simple regex that does what you need is:
ab-[a-z]*
Since you didn't provide any code I can't provide an example for how to use a regex in your context, but there are plenty of examples out there. A quick google search on how to use Regex in C# should get you started. The link I provided also has some good examples.
Related
I am looking to convert a part of a string which is substringof('has',verb) into contains(verb,'has')
As you can see, what is changing is just substring to contains and the two parameters passed to the function reversed.
I am looking for a generic solution, by using regex. Preferably using tags. i.e once i get two matches, i need to be able to reverse the matches by using $2$1 (This is how i remember doing this in perl)
You can use this regular expression code:
var re = new Regex(#"substringof\('([^']+)',([^)]+)\)");
string output = re.Replace(input, #"contains($2, '$1')");
.NET Fiddle example
You can use a regex like this:
.*?\((.*?),(.*?)\)
Working demo
Then you can use a string replacement like this:
contains(\2,\1) or
contains($2,$1)
Btw, if you just want to change the substringof, then you can use:
substringof\((.*?),(.*?)\)
I'm trying to read scripts from various .sql files into a string and execute them in SQL server using c#. I want to remove the terms USE, GO etc. I succeeded removing one word at a time but cannot find the pattern to search for two or more words at the same time.
so far,
string pattern = #"\bUSE*\b";
string script = Regex.Replace(script, pattern, "");
How to add other search terms to the pattern? i tried msdn docs and previous questions but cannot find what i need. Any help is greatly appreciated.
You can use pipe in your keywords.
string pattern = #"\b(USE|GO|MORE|FEW_MORE|AND_SO_ON)\b";
I'm a little rusty, but have you tried
string pattern = #"\bUSE*\b|\bGO*\b"
??
My c# code stores a text.
I want to fetch some words without a known pattern which appear among words with known patterns. I don't want to fetch the words with the patterns.
i.e.
My company! 02-45895438 more details: myDomain.mysite.com
can I fetch like this?
<vendorName?>\\s*\\d{2}-d{6}\\s*more details: <site?>
vendorName = "My company!" or "My company! "
site = "myDomain.mysite.com"
Is there any way to do so with regex?
from your description, it seems like you want to find "myDomain.mysite.com" from the string "My company! 02-45895438 more details: myDomain.mysite.com", if that's the case you can use a regex simmilar to this one to get the string you want
(?<=My company! 02-45895438 more details: ).*?
that should give you the substring based on the preceeding match, but will ommit that from the capture.
You can do this by using parentheses. For example, this will give you the contents of a bold tag:
<b>([^>]+)</b>
You can then use Regex.Match to get a Match object, then get the groups via Match.Groups. Each group is a set of parentheses, so in this case there's one group that contains the tag's content.
THis is the syntax I was looking for:
(?<TheServer>\w*)
like in:
string matchPattern = #"\\\\(?<TheServer>\w*)\\(?<TheService>\w*)\\";
see
http://en.csharp-online.net/CSharp_Regular_Expression_Recipes%E2%80%94Extracting_Groups_from_a_MatchCollection
I wish to known if exist a clean way to split a string using different tags for opening and ending.
For example:
<&field1&>outside<&field2&>
using the function split:
string[] dd={"<&","&>"};
string[] b1 = a1.Split(dd,StringSplitOptions.None);
it show me:
0:
1:field1
2:outside
3:field2
4:
(that it is that i want to do).
but also
<&field1<&outside<&field2<&
show the same.
#"\G<&(?<code>.*?)&>"
The TemplateParser in the AspCodeRegex class in System.Web.RegularExpressions uses something similar to this(answer via #rexm)
You should use a regular expression to do this. After a quick play I came up with this which seems to match the entries within the <& &> delimiters, but you get the idea:
<&([^&]*)&>
See Regular Expression Examples for some more examples and also the code you need to run your regex.
How can I write a regular expression to replace links with no link text like this:
with
http://www.somesite.com
?
This is what I was trying to do to capture the matches, and it isn't catching any. What am I doing wrong?
string pattern = "<a\\s+href\\s*=\\s*\"(?<href>.*)\">\\s*</a>";
I wouldn't use a regex - I'd use the Html Agility Pack, and a query like:
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[.='']")) {
link.InnerText = link.GetAttribute("href");
}
I could be wrong, but I think you simply need to change the quantifier within the href group to be lazy rather than greedy.
string pattern = #"<a\s+href\s*=\s*""(?<href>.*?)"">\s*</a>";
(I've also changed the type of the string literal to use #, for better readability.)
The rest of the regex appears fine to me. That you're not capturing any matches at all makes me think otherwise, but there could be a problem in the rest of the code (or even the input data - have you verified that?).
I would suggest
string pattern = "(<a\\b[^>]*href=\"([^\"]+)\"[^>]*>)[\\s\\r\\n]*(</a>)";
This way also links with their href attribute somewhere else would be captured.
Replace with
"$1$2$3"
The usual word of warning: HTML and regex are essentially incompatible. Use with caution, this might blow up.
Marc Gravell has the right answer, regexes are fundamentally bad at parsing HTML (see Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why). See Can you provide an example of parsing HTML with your favorite parser? for examples using a variety of parsers.