How would a regex look like when I search for this string:
before CAN be many comment lines --------"Encrypted" after must come a newline.
this does not seem to work:
Regex pattern = new Regex(#"^[-]*$[Encrypted][\n]");
what do I wrong?
The pattern you're searching for is not entirely clear to me, nor are the rest of the contents you're searching in, but if you're really just looking for "Encrypted" directly followed by only a newline then this is all you need to do:
Regex r = new Regex(#"Encrypted\n")
EDIT
Ok, comments seem to suggest that you're looking for zero or more occurences of "-", followed by "Encrypted", followed by newline. In that case the following will work.
Regex r = new Regex(#"-*Encrypted\n");
If there should be at least one "-" before "Encrypted", it will be
Regex r = new Regex(#"-+Encrypted\n");
Try it without the square brackets and move the dollar $ to the end of the pattern. Ie:
Regex pattern = new Regex(#"^-*Encrypted$");
The square brackets is like an Or statement. So [Encrypted] is the same as saying: 'E' or 'n' or 'c' or 'r'.... or 'd'.
The dollar symbol matches the end of the string.
I don't know specifically regex for c# but i thing you must not put the ^ because -'s are not at the begining of the line. And what the $ is doing in the middle.
So i would do in PCRE regex:
/-+"Encrypted" *\n/
That match one or more - followed by "Encryption" followed by 0 or more space followed by newline.
#Sanju
does this work :P
Regex pattern = new Regex(#"-*Encrypted\n");
for me it did! You have to remove the "^" char
Related
This is something simple but I cannot figure this out. I want to find a substring with this regex. It will mach "M4N 3M5", but doesn't match the below :
const string text = "asdf M4N 3M5 adsf";
Regex regex = new Regex(#"^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$", RegexOptions.None);
Match match = regex.Match(text);
string value = match.Value;
Try removing ^ and $:
Regex regex = new Regex(#"[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}", RegexOptions.None);
^ : The match must start at the beginning of the string or line.
$ : The match must occur at the end of the string or before \n at the
end of the line or string.
If you want to match only in word boundaries you can use \b as suggested by Mike Strobel:
Regex regex = new Regex(#"\b[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}\b", RegexOptions.None);
I know this question has been answered but i have noticed two thing in your pattern which i want to highlight:
No need to mention the single instance of any token.
For example: (Notice the missing {1})
\d{1} = \d
[A-Z]{1} = [A-Z]
Also I won't recommend you to enter a <space>in your pattern use '\s' instead because if mistakenly a backspace is pressed you might not
be able to figure out the mistake and running code will stop
working.
Personally, for this case i would recommend you to use \b since it is best fit here.
This regex below captures the -aaaa and -cccc but not the -eee
How can I do that?
keywords = "-aaa bbb -ccc -eee";
MatchCollection wordColNegEnd = Regex.Matches(keywords, #"-(.*?) ");
Use a "word boundary" /\b/ instead of a space, which matches the end of the string as well as a word/non-word boundary:
Regex.Matches(keywords, #"-(.*?)\b");
or, depending on what characters may be in the strings, just use "word characters" /\w/ to match the pattern:
Regex.Matches(keywords, #"-(\w+)");
MatchCollection worldColNegEnd = Regex.Matches(keywords, #"-(.*?)\b"
Word boundary is better than space, please give someone else upvotes though, since I brain farted the purpose of it.
Also I don't know why you included a ? in your original so I left it, but I believe it is not necessary, as * matches 0 or more matches.
Use
MatchCollection wordColNegEnd = Regex.Matches(keywords, #"-(.+?)\b");
Currently, your regex requires a trailing space behind the capturing group. the strings "aaa" and "ccc" have this, but "eee" does not.
Instead of matching any characters occurring after a dash, try matching nonspace characters:
#"-(\S*?)"
keywords = "-aaa bbb -ccc -eee";
MatchCollection wordColNegEnd = Regex.Matches(keywords, #"-\w+");
You haven't specified what exactly you are trying to match here.
But if I understood it right, you want to match any alpha string that starts with -
Use this RegEx: -[a-z]+
I'm new to regex and I want to highlight hexadecimal numbers in Assembly style. Like this:
$00
$FF
$1234
($00)
($00,x)
and even hexadecimal numbers that begin with #.
So far I wrote "$[A-Fa-f0-9]+" to see if it highlights numbers beginning with $ but it doesn't. Why? And can someone help me with what I'm doing? Thanks.
Put a back slash before $ and your regex will work like so
\$[A-Fa-f0-9]+
$ is a valid regex character that matches with end of string. So if your pattern contains dollar then you need to escape it. See regex reference for details
This should cover all those cases, including the cases in which you get a # instead of a $
public Regex MyRegex = new Regex(
"^(\\()?[\\$#][0-9a-fA-F]+(,x)?(?(1)\\))[\\s]*$",
RegexOptions.Singleline
| RegexOptions.Compiled
);
The unescaped sequence for the single line: ^(\()?[\$#][0-9a-fA-F]+(,x)?(?(1)\))[\s]*$
That should validate on a per-line match.
By the way, I made this regex pretty quickly using Expresso
I want a Regular Expression for a word.otherword form. I tried \b[a-z]\.[a-z]\b, but it gives me an error at the \. part, saying Unrecognized escape sequence. Any idea what's wrong? I'm working under .NET C#. Thanks!
LE:
john.Smith or JoHn.SmItH or JOHN.SMITH should work.
John Smith or john!Smith or john.Smith.Smith shouldn't work.
Try this :
foundMatch = Regex.IsMatch(SubjectString, #"\b[a-z]\.[a-z]\b");
Probably you were not using #?
Your regex tries to match a.a this means a single character. But since you want it to match complete words you need a quantifier e.g.
\b[a-z]+\.[a-z]+\b
Finally you may want to use the case insensitive match to allow for words with capital letters to be matched too :
foundMatch = Regex.IsMatch(SubjectString, #"\b[a-z]+\.[a-z]+\b", RegexOptions.IgnoreCase);
This will match all words.words with at least one character for each word regardless of capitalization.
This will match all word.otherword only if there is a space behind the first word or it is the start of the string and only if there is a space after the second word or it is the end of the string.
foundMatch = Regex.IsMatch(SubjectString, #"(?<=\s|^)\b[a-z]+\.[a-z]+\b(?=\s|$)", RegexOptions.IgnoreCase);
Try this regex for word.word format:
#"\b([a-z]+)\.\1"
For word.otherword use this:
#"\b[a-z]+\.[a-z]+\b"
I am trying to create a regular expression pattern in C#. The pattern can only allow for:
letters
numbers
underscores
So far I am having little luck (i'm not good at RegEx). Here is what I have tried thus far:
// Create the regular expression
string pattern = #"\w+_";
Regex regex = new Regex(pattern);
// Compare a string against the regular expression
return regex.IsMatch(stringToTest);
EDIT :
#"^[a-zA-Z0-9\_]+$"
or
#"^\w+$"
#"^\w+$"
\w matches any "word character", defined as digits, letters, and underscores. It's Unicode-aware so it'll match letters with umlauts and such (better than trying to roll your own character class like [A-Za-z0-9_] which would only match English letters).
The ^ at the beginning means "match the beginning of the string here", and the $ at the end means "match the end of the string here". Without those, e.g. if you just had #"\w+", then "##Foo##" would match, because it contains one or more word characters. With the ^ and $, then "##Foo##" would not match (which sounds like what you're looking for), because you don't have beginning-of-string followed by one-or-more-word-characters followed by end-of-string.
Try experimenting with something like http://www.weitz.de/regex-coach/ which lets you develop regex interactively.
It's designed for Perl, but helped me understand how a regex works in practice.
Regex
packedasciiRegex = new Regex(#"^[!#$%&'()*+,-./:;?#[\]^_]*$");