this one is really easy.
I'm trying to create a Regular Expression that will result in a Successful Match when against the following text
/default.aspx?
So i tried the following...
^/default.aspx$
and it's failing to match it.
Can someone help, please?
(i'm guessing i'm screwing up becuase of the \ and the ? in the input expression).
The problem is in the .(dot), which is a wildcard,
You must escape it like \..
Also, Because there is a ? at the end of URL and $ (end-of-input) is in the regexp, therefore, it does not match.
The correct regexp should be ^/default\.aspx(\?.*)?$
The $ at the end of ^/default.aspx$ means 'match the end of the string', but the string you're searching ends with '?'.
Maybe something like this is more appropriate:
^/default\.aspx(\?.*)?$
This will match default.aspx, with an optional ?whatever-else-that-comes-after.
Related
I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?
I have a simple python regex re.search('<span[\w\W]*?>Members[\w\W]*?([\w\W]*?)</span>', str). I'd like to do the same in ruby.
From the docs, it appears that match should work. However when I try
/<span[\w\W]*?>Members[\w\W]*?([\w\W]*?)</span>/.match(str) I get a syntax error.
I know this is something obvious but would love some help. Thank you
You need to escape the / which is inside closing tag of </span>
/<span[\w\W]*?>Members[\w\W]*?([\w\W]*?)<\/span>/.match(str)
// ^^
otherwise this will be considered as end of regex
and you can use .*? where . mean capture anything except line break
/<span.*?>Members.*?(.*?)<\/span>/.match(str)
If you have a lot of slashes to match, try %r notation:
%r{<span[\w\W]*?>Members[\w\W]*?([\w\W]*?)</span>}.match(str)
I have an example string:
*DataFromAdHoc(cbgv)
I would like to extract by RegEx:
DataFromAdHoc
So far I have figured something like that:
^[^#][^\(]+
But Unfortunately without positive result. Do you have maybe any idea why it's not working?
The regex you tried ^[^#][^\(]+ would match:
From the beginning of the string, it should not be a # ^[^#]
Then match until you encounter a parenthesis (I think you don't have to escape the parenthesis in a character class) [^\(]+
So this would match *DataFromAdHoc, including the *, because it is not a #.
What you could do, it capture this part [^\(]+ in a group like ([^(]+)
Then your regex would look like:
^[^#]([^(]+)
And the DataFromAdHoc would be in group 1.
Use ^\*(\w+)\(\w+\)$
It just gets everything between the * and the stuff in brackets.
Your answer may depend on which language you're running your regex in, please include that in your question.
I'm trying to extract a simple string from the HTML response.
The response looks like this
patients-list-of-visits.aspx?p=a1363839-76fb-43f3-97ba-26218faefee1
The Regex I have tried so far are
patients-list-of-visits.aspx?p=(.+?)
patients-list-of-visits.aspx?p=(.+)
Can someone please let me know what am I doing wrong here?
Thanks!
This is better:
patients-list-of-visits\.aspx\?p=(.+)
2 remarks
don't forget to escape . and ? if you want to match them literally
your first attempt .*? is a lazy match and will result in in only the first letter being matched. Your second attempt is better
Is it possible to have this done with one regex?
I need to match only those strings that have exactly one period/dot but the restriction is that that period/dot must not be at the end of the string.
Example:
abc.d will match
.abcd will match
abcd. will not match
Yes, you can do it in one regex:
^[^.]*\.[^.]+$
I really like #codaddict's answer, but how about something without Regex? ( C# code below )
if(a.Split('.').Length>2 || a.EndsWith("."))
{
Console.WriteLine("invalid");
}
What I like is that it is much more clear that you don't want a string with two . and also a . should not be in the end. And this might actually be faster than using a regex.