I have a problem with writting a reg exp for a string like this in C#
String correct = "<a>link</a>";
String wrong = "link</a>";
I know how to select the first in a reg exp example
string regExp = "^(<a>)";
Ans i know how to select the last one
string regExp = "(</a>)$";
But how could i combine this two, to one
Please use:
Regex regex = new Regex("<a>(.*)</a>");
string correct = "<a>link</a>";
bool okBool = regex.IsMatch(correct); // true
string wrong = "link</a>";
bool wrongBool = regex.IsMatch(wrong); //false
Or as mentioned by Ilya Ivanov, you can use this regex:
Regex regex = new Regex("^<a>(.*)</a>$");
Related
I want to find out, whether my string contains a text like #1, #a, #abc, #123, #abc123dsds and so on... ('#' character with one or more characters (digits and letters).
My code so far won't work:
string test = "#123";
boolean matches = test.Contains("#.+");
The matches variable is false.
String.Contains does not accept a regex.
Use Regex.IsMatch:
var matches = Regex.IsMatch(test, "#.+");
test.Contains("#.+"); does not "understand" regular expressions. It literally checks if the string test literally contains #.+ sequence of characters, which #123 does not contain.
Use Regex.IsMatch instead:
bool matches = Regex.IsMatch(test, "#.+");
Demo.
Or without regex, you can use a combination of StartsWith, Enumerable.Any and char.IsLetterOrDigit methods like;
var s = "#abc123dsds+";
var matches = s.Length > 1 && s.StartsWith("#") && s.Substring(1).All(char.IsLetterOrDigit);
You need to use Regex in order to use a regex pattern.
string text = "#123";
Regex rgx = new Regex("#[a-zA-Z0-9]+");
var match = rgx.Match(text);
bool matche = match.Success)
Well this worked for me. \# checks if it starts with #, \w checks if it is a word.
class Program
{
static void Main(string[] args)
{
string text = "#2";
string pat = #"\#(\w+)";
Regex r = new Regex(pat);
Match m = r.Match(text);
Console.WriteLine(m.Success);
Console.ReadKey();
}
}
I have this string here
string Thing1 = "12340-TTT";
string Thing2 = "®"
I am looking to use reg replace to replace the TTT with ®.
I am told using reg replace it does not matter if its uppercase or lowercase.
How would I go about doing this?
string input = "12340-TTT";
string output = Regex.Replace(input, "TTT", "®");
// Write the output.
Console.WriteLine(input);
Console.WriteLine(output);
Console.ReadLine();
This should do the trick. You find "TTT" in a string and replace it with "®".
Try this:
string one = "1234-TTT";
string pattern = "TTT";
Regex reg = new Regex(pattern);
string two = "®";
string result = reg.Replace(one, two);
Console.WriteLine(result);
should give you the desired Result. And just for a good read if you should ever need some more complicated Regular Expressions: http://msdn.microsoft.com/en-us/library/vstudio/xwewhkd1.aspx
correct me if i'm wrong but i think it is same with that of replacing a string on a variable like this:
string Thing1 = "12340-TTT";
string Thing2 = Regex.Replace(Thing1 , "®", "anyString");
got it from here:
http://www.dotnetperls.com/regex-replace
cheers:)
For this, you can use the Regex.Replace method (documentation here)
There are several overloaded versions, but the most direct one for this is the Regex.Replace(String input, String regexPattern, String replacementString) version:
string Thing1 = "12340-TTT";
string Thing2 = "®";
string newString = System.Text.RegularExpressions.Regex.Replace(Thing1, "[Tt]{3}", Thing2);
If you are unfamiliar with regular expressions, the [Tt] define specific character group (any characters matching one of ones specified) and the {3} just indicates that it must be appear 3 times. So, this will do a case-insensitive search for a string of 3 T's (e.g., TTT, ttt, TtT, tTt, etc.)
For more on basic regex syntax, you can look here: http://www.regular-expressions.info/reference.html
Also, between the RegexOptions you can pass to the regex there is the IgnoreCase to make it case insensitive:
string Thing1 = "12340-TTT";
string Thing2 = "®"
var regex = new Regex("TTT", RegexOptions.IgnoreCase );
var newSentence = regex.Replace( Thing1 , Thing2 );
I have a string and need the letters from said string.
string s = "EMA123_33"; // I need "EMA"
string s = "EMADRR123_33"; // I need "EMADRR"
I am using C# in Visual Studio 2008.
You can try this:
var myString = "EMA123_33";
var onlyLetters = new String(myString.Where(Char.IsLetter).ToArray());
please note: this version will find "e" just like "E" - if you need only upper-case letters then do something like this:
var myString = "EMA123_33";
var onlyLetters = new String(myString.Where(c => Char.IsLetter(c) && Char.IsUpper(c)).ToArray());
You can use a regular expression to replace all non-letters:
string s2 = Regex.Replace(s, #"[^A-Z]+", String.Empty);
If you're just after the initial letters, i.e. those at the start of the string (your examples are a bit unclear in that I don't know what would happen to letters at the end of the string), you can use a different Regex:
string s2 = Regex.Replace(s, #"(\p{L}+).*", "$1");
Regex MyRegex = new Regex("[^a-z]", RegexOptions.IgnoreCase);
string s = MyRegex.Replace(#"your 76% strings &*81 gose _ here and collect you want_{ (7 438 ?. !`", #"");
Console.WriteLine(s);
output
yourstringsgosehereandcollecyouwant
How do I write a regular expression to match (_Rev. n.nn) in the following filenames (where n is a number):
Filename_Rev. 1.00
Filename_Rev. 1.10
Thanks
The following should work (for the whole line):
#"^Filename_Rev\.\s\d\.\d\d$"
Should capture versions >9
Edit: Fixed
string captureString = "abc123butts_Rev. 1.00";
Regex reg = new Regex(#"(.(?!_Rev))+\w_Rev\. (?<version>\d+\.\d+)");
string version = reg.Match(captureString).Groups["version"].Value;
Building off of #leppie's answer (give him the green check not me), you can extract the numbers from your regex match by putting parens around the \d's.
Regex foo = new Regex(#"_Rev\.\s(\d)\.(\d\d)$");
GroupCollection groups = foo.Match("Filename_Rev. 1.00").Groups;
string majorNum = groups[1].Value;
string minorNum = groups[2].Value;
System.Console.WriteLine(majorNum);
System.Console.WriteLine(minorNum);
I'm bulding document-from-template engine. At certain points I need to match on Reg Exp groups and replace template text with content from a db.
I 'hardcoded' my RegExp initially, doing something like:
Regex r = new Regex(#"{DocSectionToggle::(?<ColumnName>\w+)::(?<ResponseValue>.+)}\n\[\[(?<SectionContent>.+)\]\]", RegexOptions.Multiline);
Apologies: it does group capture, so the syntax isn't the prettiest.
Just to make things neater and because I want' to keep the patterns in web.config or elsewhere, I've 'evolved' algorithm to something like:
string _regexp_DocSectionToggle = #"{DocSectionToggle::{0}::{1}}\n\[\[{2}\]\]";
/* Reg Exp Patterns for group capture */
string _rxCol = #"(?<{ColumnName}>\w+)";
string _rxResp = #"(?<{ResponseValue}>.+)";
string _rxSectContent = #"(?<{SectionContent}>.+)";
Regex r = new Regex( string.Format(_regexp_DocSectionToggle,
_rxCol,
_rxResp,
_rxSectContent),
RegexOptions.Multiline
);
But I'm getting an error: 'Input string was not in correct format'.
Can anyone tell my why? Is this a limitation of string.Format(...)?
Thanks for looking.
The problem is the { and } which you don't want to mark format specifiers. IIRC, you just double them:
string _regexp_DocSectionToggle = #"{{DocSectionToggle::{0}::{1}}}\n\[\[{2}\]\]";
You need to escape { and } by using {{ and }} as the following:
string _regexp_DocSectionToggle = #"{{DocSectionToggle::{0}::{1}}}\n\[\[{2}\]\]";