I need a piece of regex that can be used to do a NOT match. Take for example the following URL's
http://www.site.com/layout/default.aspx
http://www.site.com/default.aspx
http://www.site.com/layout.aspx
The regex should NOT match any url string that contains the directory "layout"
http://www.site.com/layout/default.aspx
and instead should match on
http://www.site.com/default.aspx
http://www.site.com/layout.aspx
How can i do this using .NET regex?
Use negative lookahead:
^(?!.*/layout/)
You have to anchor to the start of the string or you'll get false positives i.e. (?!/layout/) alone won't work.
If you need to squeeze everything in one regex, try negative lookahead, something along the lines of this:
(?!layout)
Just match /layout/ and invert the result, whatever language you use.
E.g. with PHP:
if(!preg_match('#/layout/#i', $url)) {
// does not match layout
}
PYTHON:
import re
if not re.match('layout'):
#do whatever here
re is regex for python
Related
i just want to match this string with regex:
How can i do this?
/profil
i try do this in this way:
.*/profil.*
But my software dont match any results in text.
All you need is this
#"(\/profil)"
if all you need is to match "/profil" then there is no need for the ".*"
Remember the expressions are greedy.
The . matches any character, and * tells it that this . can go on forever. So this expression will eat up all of your input, leaving nothing for the /profil part.
It seems like you're trying to put a wildcard around /profil. This is not needed with regular expressions. You should just be able to use /profil as the full expression and match your string.
I am extracting all numbers used in an xml file. The numbers are written in following two patterns
<Environment Id="11" StringId="8407" DescriptionId="5014" RemoteControlAppStringId="8119; 8118" EnvironmentType="BlueToothBridge" AlternateId="1" XML_NAME_ID="BTBSpeechPlusM" FactoryGainType="LIN18">
<Offsets />
</Environment>
I am using regex: "\"\d*;\"" and "\"\d*\"" to extract all numbers.
from the above when i ran Regex "\"\d*\"" using
Regex.Match(myString, "\"\\d*\"")
the above line returns 8407, 11,5014 but it is not returning 8119 and 8118
Your regex will fail to match 8119; 8118 because your pattern is finding quoted numbers.
try with
\b\d+\b
\b specify that \d+ will match only in word boundary. So LIN18 will not match.
Depening on whether you can assume that the provided input is valid XML, you could use the following regular expression:1
Regex.match(myString, "(?<=\")\\d+(?=\")|(?<=\")\\d+(?=; ?\\d+\")|(?<=\"\\d+; ?)\\d+(?=\")" )
The main idea behind this is that it takes the three possible situations into account:
"[number]"
"[number]; [other_number]" (With or without a space before [other_number])
"[other_number]; [number]" (With or without a space before [number])
There are two new concepts I included in the regular expression:2
Positive lookahead: (?=[regex])
Positive lookbehind: (?<=[regex])
These concepts allow the regular expression to check if something specific is before or after it, without putting it in the match.
This regular expression could easily be optimised, but this is meant as an example of a basic approach.
One good tip for developing a regular expression like this is to use a tool (online or offline) to test your regular expression. The tool I used was .NET Regex Tester.
As #poke stated in the comment, it's because your regex doesn't match the string. Change your regex to capture specific matches and account for the possibility of the ';'.
Something like below should probably do the trick.
EDIT: (\b\d+\b)|(\b\d+[;*]\d+\b)
I have a string like 30+20%. Now I want to replace 20% with (20/100). Thats it.
If the percent doesn't occur in any other situation in the string, you don't even need a regular expression:
s = s.Replace("%", "/100");
To add the parentheses you need the regular expression though:
s = Regex.Replace(s, #"(\d{1,3})%", "($1/100)");
string s="30+20%";
s=s.Replace("%","/100)");
s=s.Replace("+","+(");
I'll just assume you run Perl
input="30+20%"
echo $input | perl -pe 's#(\d+)%#\($1/100\)#g'
EDIT: just read the tags, anyways, the regex should work in C#
That should be an easy regex to try. 1 to 3 digits followed by a percentage sign.
You need to capture the 1-3 digits group for backreference, and use it to create
(DIGITS/100) string.
You can play here :http://gskinner.com/RegExr/ to learn regexes.
I'm not sure what programming language are you using but this is how you would do this in python:
import re
re.sub(r'(\d*)%', r'\1/100', '30+20%')
The returned string will be '30+20/100'.
Explanation:
Let's look at the regex. r'\d*%' is a regex that matches a series of digits followed by the % sign. I put paranthesis arount (\d*) to tell the regex compiler that the series of digits (aka the number) is the first group. The second arguemnt tells the sub functions how to replace the matched string. The argument '\1/100' tells the sub function I want it to replace the matched string with the value of the first group matched by the regex (through the \1 part) followed by /100.
You can check the python re module for more information.
Try this
string resultString = null;
try {
resultString = Regex.Replace(subjectString, #"\b(\d+)%", "($1/100)");
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
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");
Aloha,
I need to match page.aspx?cmd=Show&id= but not match http://random address/page.aspx?cmd=Show&id=
Right now I'm just doing a simple resting replace with strContent.Replace("page.aspx?cmd=Show&id=", "page.aspx/Show/"); but to avoid the above i think I need to move to regular expressions.
Edit: Sorry i was a little short on details The current replace works because I was not taking into account that the address being modified should only be relative addresses, not absolute. I need to catch all relative addresses that match page.aspx?cmd=Show&id= and convert that string into page.aspx/Show/.
The .StartsWith and ^ wont work because I an looking in a string that is a full html page. So I need to be able to convert:
<p>Hello, please click here.</p>
into
<p>Hello, please click here.</p>
but not convert
<p>Hello, please click here.</p>
I used an A tag for my example but I need to convert IMG tags as well.
Thanks!
I would suggest using ParseQueryString rather than a regular expression because this will work even if the parameters are in a different order.
Otherwise you can use string.StartsWith to test if there is a match.
If you want to use a regular expression you need to use ^ and also escape all the special characters in your string:
Regex regex = new Regex(#"^page\.aspx\?cmd=Show&id=");
If you don't want to escape the characters yourself you can get Regex.Escape to do it for you:
string urlToMatch = "page.aspx?cmd=Show&id=";
Regex regex = new Regex("^" + Regex.Escape(urlToMatch));
You can just use a ^ to look for start of line.
... Or you can be cool and use a negative lookbehind (?<!http://.*?/). ;)
See http://www.geekzilla.co.uk/View6BD88331-350D-429F-AB49-D18E90E0E705.htm