I would like to find some words not surrounded by any numerics or alphanumerics or surrounded by nothing.
So if I'm looking for Foo123 I would like this result
="Foo123"; =>TRUE
barFoo123; =>FALSE
Foo123 =>TRUE
BarBar123Foo123Bar; =>FALSE
;Foo123 =>TRUE
I just built this expression:
(^[^0-9a-zA-Z]?)WORDTOFIND([^0-9a-zA-Z]?$)
I was pretty sure I'm in the right way but when I'm using it like this:
if (Regex.IsMatch(line, string.Format(#"(^[^0-9a-zA-Z]?){0}([^0-9a-zA-Z]?$)",snCode)) )
{
}
It doesn't work. What am I doing wrong?
You're essentially looking for
(?<![a-zA-Z0-9])Foo123(?![a-zA-Z0-9])
This uses a lookahead and lookbehind to make sure that there isn't an alphanumeric character either before or after Foo123. This assumes ASCII, though.
The following matches the strings you do NOT want so do !Regex.IsMatch()
(^([0-9a-zA-Z])+{0}.*)|(.*{0}([0-9a-zA-Z])+)
Related
I'm guessing that I'm confused about how groups work in regexp.
My regexp replaces more characters then it should.
Here is my string:
...test - Copy\asd.test2\asd.keke
And here is my pattern:
.?(asd\.)
It matches "\asd." but I want it to match just "asd."
What am I getting wrong here?
What are you trying to achieve using .? if you don't want to match it?
To check for characters outside of your match, you can use lookaround assertion. E.g. checking for a backslash before the match, you'd use
(?<=\\)asd\.
I have the two following strings
/signon/default.wl?rs=WLW11.10&vr=2.0&fn=_top
/signon/default.wl?fn=%5Ftop&newdoor=true&rs=WLW11%2E10&vr=2%2
I would like to match all the strings except the ones that do not contain newdoor
so far i have the following regex
/signon/default.wl\?(?=[\w]*)(?!newdoor)
but it matches all strings.
can anyone point out what im doing wrong.
You can try this /signon/default.wl\?(?!.*newdoor).*
It asserts using negative lookahead that there is no occurrence of newdoor in the input string. Code will look like this
resultString = Regex.Match(subjectString, #"/signon/default.wl\?(?!.*newdoor).*",
RegexOptions.IgnoreCase).Value;
/signon/default.wl\?(?=[\w]*)(?!newdoor)
Both (?=) and (?!) are zero-width assertions that don't consume any input. Try:
/signon/default.wl\?(.(?!newdoor))*$
why not use something like:
subjectString.contains("newdoor");
I'm currently facing a (little) blocking issue. I'd like to replace a substring by one another using regular expression. But here is the trick : I suck at regex.
Regex.Replace(contenu, "Request.ServerVariables("*"))",
"ServerVariables('test')");
Basically I'd like to replace whatever is between the " by "test". I tried ".{*}" as a pattern but it doesn't work.
Could you give me some tips, I'd appreciate it!
There are several issues you need to take care of.
You are using special characters in your regex (., parens, quotes) -- you need to escape these with a slash. And you need to escape the slashes with another slash as well because we 're in a C# string literal, unless you prefix the string with # in which case the escaping rules are different.
The expression to match "any number of whatever characters" is .*. In this case, you would want to match any number of non-quote characters, which is [^"]*.
In contrast to (1) above, the replacement string is not a regular expression so you don't want any slashes there.
You need to store the return value of the replace somewhere.
The end result is
var result = Regex.Replace(contenu,
#"Request\.ServerVariables\(""[^""]*""\)",
"Request.ServerVariables('test')");
Based purely on my knowledge of regex (and not how they are done in C#), the pattern you want is probably:
"[^"]*"
ie - match a " then match everything that's not a " then match another "
You may need to escape the double-quotes to make your regex-parser actually match on them... that's what I don't know about C#
Try to avoid where you can the '.*' in regex, you can usually find what you want to get by avoiding other characters, for example [^"]+ not quoted, or ([^)]+) not in parenthesis. So you may just want "([^"]+)" which should give you the whole thing in [0], then in [1] you'll find 'test'.
You could also just replace '"' with '' I think.
Taryn Easts regex includes the *. You should remove it, if it is just a placeholder for any value:
"[^"]"
BTW: You can test this regex with this cool editor: http://rubular.com/r/1MMtJNF3kM
Pattern is
Regex splRegExp = new System.Text.RegularExpressions.Regex(#"[\,#,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,-,_]");
All characters work except '-'. Please advise.
Use
#"[,#+\\?\d%.*&^$(!)#_-]"
No need for all those commas.
If you place a - inside a character class, it means a literal dash only if it's at the start or end of the class. Otherwise it denotes a range like A-Z. As Damien put it, the range ,-, is indeed rather small (and doesn't contain the -, of course).
'-' has to be the first charater in your regex.
Regex splRegExp = new System.Text.RegularExpressions.Regex(#"[-,\,#,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,_]");
You need to escape the -character for it to work (it's a regular expression syntax)
Try this:
"[\,#,+,\,?,\d,%,.,?,*,&,^,$,(,!,),#,\-,_]"
I am trying to use Regex to find out if a string matches *abc - in other words, it starts with anything but finishes with "abc"?
What is the regex expression for this?
I tried *abc but "Regex.Matches" returns true for xxabcd, which is not what I want.
abc$
You need the $ to match the end of the string.
.*abc$
should do.
So you have a few "fish" here, but here's how to fish.
An online expression library and .NET-based tester: RegEx Library
An online Ruby-based tester (faster than the .NET one) Rubular
A windows app for testing exressions (most fully-featured, but no zero-width look-aheads or behind) RegEx Coach
Try this instead:
.*abc$
The $ matches the end of the line.
^.*abc$
Will capture any line ending in abc.
It depends on what exactly you're looking for. If you're trying to match whole lines, like:
a line with words and spacesabc
you could do:
^.*abc$
Where ^ matches the beginning of a line and $ the end.
But if you're matching words in a line, e.g.
trying to match thisabc and thisabc but not thisabcd
You will have to do something like:
\w*abc(?!\w)
This means, match any number of continuous characters, followed by abc and then anything but a character (e.g. whitespace or the end of the line).
If you want a string of 4 characters ending in abc use, /^.abc$/