I have the following C# regex
#"(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube\.com\/watch\?[^?]*v=|youtu\.be\/)))([\w-]+)";
How can I correct this so the regex won't match URLs with double quote at the beginning of the URL. so if the URL is in an href attribute in an hyperlink, it will be ignored and not captured.
I've used this expression in my other Twitter Regex pattern, but I can't make it work in this one.
(?<!"")
It worked on the Twitter pattern:
(?<!"")https?://twitter\.com/(?:#!/)?(\w+)/status(?:es)?/(\d+)
So the YouTube Regex should grab only URLs that are not with double quotes in the beginning of the URL.
To answer the question: (?<!") will fail a match if there is no " immediately before the current location. If there must be no " followed with 0+ other chars before the current location, you may leverage .NET infinite-width lookbehind.
In this case, you might want to turn your loobehind into
(?<!"[^"<>]*)
See the regex demo. Note that [^"<>]* matches 0+ chars other than ", < and >, so, the " will be checked only when inside an element node if the HTML is perfectly serialized. If it contains plain < or > inside attribute values, this approach won't work.
That is why you should think about using an appropriate HTML parser for this task, too, since you are using it already in the project. If you let know what you are trying to achieve, I will update the answer.
Related
I am actually trying to find all the URLs in string. There are many similar types of questions here in this website but nothing matches exactly what I want. My text may contain URLs as well as other text with dots. An example is shown below:
This is a text. It may contain links such as https://stackoverflow.com/, www.test.com but it may also contain other things such as exampleimage.this.png or picture.man.jpeg which is not a URL. On the other hand it many contain URLs without protocol such as example.com
So in the text above, I would only like to get the urls, so mainly -
https://stackoverflow.com/
www.test.com
example.com
But I should not get exampleimage.this.png or picture.man.jpeg as a url.
I have tried
"(?:(?:https?|ftp)://)?[\w/-?=%.]+.[\w/-&?=%.]+"
which gives me all the urls except example.com.
And I have also tried
"(ftp://|www.|https?://){1}[a-zA-Z0-9u00a1-\uffff0-]{2,}.[a-zA-Z0-9u00a1-\uffff0-]{2,}(\S*)"
"(?:(?:https?|ftp)://|\b(?:[a-z\d]+.))(?:(?:[^\s()<>]+|((?:[^\s()<>]+|(?:([^\s()<>]+)))?))+(?:((?:[^\s()<>]+|(?:(?:[^\s()<>]+)))?)|[^\s`!()[]{};:'"".,<>?«»“”‘’])"
which gives me all the urls with the exampleimage.this.png and picture.man.jpeg which is not what I want.
Could anybody please help me out? Anything other than Regex would also be fine.
I am using C# with Regex for this.
You need to match up to a valid TLD:
var pattern = #"(?i)\b(?:(?:http|ftp)s?://|www\.)?[\w/?=%.-]*?\.(?:A(?:A(?:A|RP)|B(?:ARTH|B(?:OTT|VIE)?|C|LE|OGADO|UDHABI)|C(?:ADEMY|C(?:ENTURE|OUNTANTS?)|O|TOR)?|D(?:AC|S|ULT)?|E(?:G|RO|TNA)?|F(?:L|RICA)?|G(?:AKHAN|ENCY)?|I(?:G|R(?:BUS|FORCE|TEL))?|KDN|L(?:FAROMEO|I(?:BABA|PAY)|L(?:FINANZ|STATE|Y)|S(?:ACE|TOM))?|M(?:AZON|E(?:RICAN(?:EXPRESS|FAMILY)|X)|FAM|ICA|STERDAM)?|N(?:ALYTICS|DROID|QUAN|Z)|OL?|P(?:ARTMENTS|P(?:LE)?)|Q(?:UARELLE)?|R(?:A(?:B|MCO)|CHI|MY|PA|TE?)?|S(?:DA|IA|SOCIATES)?|T(?:HLETA|TORNEY)?|U(?:CTION|DI(?:BLE|O)?|SPOST|T(?:HOR|OS?))?|VIANCA|WS?|XA?|Z(?:URE)?)|B(?:A(?:BY|IDU|N(?:A(?:MEX|NAREPUBLIC)|[DK])|R(?:C(?:ELONA|LAY(?:CARD|S))|EFOOT|GAINS)?|S(?:E|KET)BALL|UHAUS|YERN)?|B(?:[CT]|VA)?|C[GN]|D|E(?:A(?:TS|UTY)|ER|NTLEY|RLIN|ST(?:BUY)?|T)?|[FG]|H(?:ARTI)?|I(?:BLE|D|KE|NGO?|[OZ])?|J|L(?:ACK(?:FRIDAY)?|O(?:CKBUSTER|G|OMBERG)|UE)|M[SW]?|N(?:PPARIBAS)?|O(?:ATS|EHRINGER|FA|M|ND|O(?:K(?:ING)?)?|S(?:CH|T(?:IK|ON))|T|UTIQUE|X)?|R(?:ADESCO|IDGESTONE|O(?:ADWAY|KER|THER)|USSELS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W7B)|VR189M)|K(?:CRX77D1X4A|P(?:R(?:W13|Y57)D|UT3I))|L(?:1ACC|GBBAT1AD8J)|M(?:GB(?:9AWBF|A(?:3A(?:3EJT|4F16A)|7C0BBN0A|A(?:KC7DVF|M7A8H)|B2BD|H1A3HJKRD|I9AZGQP6J|YH7GPA)|BH1A(?:71E)?|C(?:0A9AZCG|A7DZDO|PQ6GPA1A)|ERP4A5D4AR|GU82A|I4ECEXP|PL2FH|T(?:3DHD|X2B)|X4CD0AB)|IX891F|K1BU44C|XTQ1M)|N(?:GB(?:C5AZD|E9E0A|RX)|ODE|QV7F(?:S00EMA)?|YQY26A)|O(?:3CW4H|GBPF8FL|TU796D)|P(?:1A(?:CF|I)|GBS0DH|SSY2U)|Q(?:7CE6A|9JYB4C|CKA1PMC|XA(?:6A|M))|R(?:HQV96G|OVU88B|VC1E0AM3E)|S(?:9BRJ9C|ES554G)|T(?:60B56A|CKWE|IQ49XQYJ)|UNUP4Y|V(?:ERMGENSBERAT(?:ER-CT|UNG-PW)B|HQUV|UQ861B)|W(?:4R(?:85EL8FHU5DNRA|S40L)|GB(?:H1C|L6A))|X(?:HQ521B|KC2(?:AL3HYE2A|DL3A5EE0H))|Y(?:9A3AQ|FRO4I67O|GBI2AMMX)|ZFR164B)|XX|YZ)|Y(?:A(?:CHTS|HOO|MAXUN|NDEX)|E|O(?:DOBASHI|GA|KOHAMA|U(?:TUBE)?)|T|UN)|Z(?:A(?:PPOS|RA)?|ERO|IP|M|ONE|UERICH|W))\b(?!\.\w)/?";
var output = Regex.Matches(text, pattern).Cast<Match>().Select(x => x.Value);
See the regex demo. Details:
(?i) - case insensitive matching ON
\b - a word boundary
(?:(?:http|ftp)s?://|www\.)? - an optional sequence of http or ftp followed with an optional s and then ://, or www.
[\w/?=%.-]*? - zero or more word, /, ?, =, %, . or - chars as few as possible
\. - a . char
(?:<TLD_PATTERN>) - a pattern that matches any TLD (listed in the IANA's TLD DB)
\b - a word boundary
-(?!\.\w) - fail the match if there is . and a word char immediately to the right of the current location
/? - an optional / char.
I have a regex to match the following:
somedomain.com/services/something
Basically I need to ensure that /services is present.
The regex I am using and which is working is:
\/services*
But I need to match /services OR /servicos. I tried the following:
(\/services|\/servicos)*
But this shows 24 matches?! https://regex101.com/r/jvB1lr/1
How to create this regex?
The (\/services|\/servicos)* matches 0+ occurrences of /services or /servicos, and that means it can match an empty string anywhere inside the input string.
You can group the alternatives like /(services|servicos) and remove the * quantifier, but for this case, it is much better to use a character class [oe] as the strings only differ in 1 char.
You want to use the following pattern:
/servic[eo]s
See the regex demo
To make sure you match a whole subpart, you may append (?:/|$) at the pattern end, /servic[eo]s(?:/|$).
In C#, you may use Regex.IsMatch with the pattern to see if there is a match in a string:
var isFound = Regex.IsMatch(s, #"/servic[eo]s(?:/|$)");
Note that you do not need to escape / in a .NET regex as it is not a special regex metacharacter.
Pattern details
/ - a /
servic[eo]s - services or servicos
(?:/|$) - / or end of string.
Well the * quantifier means zero or more, so that is the problem. Remove that and it should work fine:
(\/services|\/servicos)
Keep in mind that in your example, you have a typo in the URL so it will correctly not match anything as it stands.
Here is an example with the typo in the URL fixed, so it shows 1 match as expected.
First off you specify C# (really .Net is the library which holds regex not the language) in this post but regex101 in your example is set to PHP. That is providing you with invalid information such as needed to escape a forward slash / with \/ which is unnecessary in .Net regular expressions. The regex language is the same but there are different tools which behave differently and php is not like .Net regex.
Secondly the star * on the ( ) is saying that there may be nothing in the parenthesis and your match is getting null nothing matches on every word.
Thirdly one does not need to split the whole word. I would just extract the commonality in the words into a set [ ]. That will allow the "or-ness" you need to match on either services or servicos. Such as
(/servic[oe]s)
Will inform you if services are found or not. Nothing else is needed.
I have a regex for validating a string but it doesn't accept semicolons? Is it because I have to use some escape sequences? I tested my regex here and it passes i.e allows semi-colon but doesn't allow in my c# app.
EDITED I have following regex
^[A-Za-z0-9]{1}[A-Za-z.&0-9\s\\-]{0,21}$
And tried validating sar232 trading inc;
The & entity hints at the fact you have this regular expression inside some XML attribute, and that this & gets parsed as a single & symbol when the pattern is sent to the regex engine.
That means, your pattern lacks the semi-colon inside the second character class, and that is why your regex does not match the string you provided.
The solution is simple: add the semi-colon to the 2nd character class:
someattr="^[A-Za-z0-9][;A-Za-z.&0-9\s\\-]{0,21}$"
^
See the regex demo
Please also note that the {1} limiting quantifier is redundant since a [A-Za-z0-9] already matches only 1 symbol from the indicated ranges.
I have a xml file. As per my requirement I need to update empty tag such as I need to change <xml></xml> to <xml/>. Is it possible to change the tags like that..
Thank you...
var xmlString="<xml></xml> <toto></toto>";
var properString=System.Text.RegularExpressions.Regex.Replace(xmlString, "<([^>]+)></[^>]+>", "<$1/>");
EDIT: explanation!
#Neil Knight has already provided, in a comment, a link to Wikipedia explaining the concept of regular expressions. The part specific to .NET is available here: .NET Framework Regular Expressions
A starting XML tag can be matched with the following regular expression: <[^>]+>. The [^>]+ part can be read as: all characters that are not ">", with at least one character (so <> is not matched but <a> is). An ending XML tag can be matched with the same kind of expression: </[^>]+> (note the slash after the first character). So the regular expression <[^>]+></[^>]+> matches empty tags such as <foo></foo> (but be careful, it also matches <foo></bar> which is not valid XML code).
What we need now is to isolate the characters between "<" and ">". For that, we use parenthesis: <([^>]+)>. This instructs the regular expression engine to capture the matched characters. Each group of parenthesis can be referred later in a replacement operation by the "$x" string (where "x" is a number: "$1" for the first matching parenthesis, "$2" for the second one, etc.).
So, with a call to Regex.Replace(xmlString, "<([^>]+)></[^>]+>", "<$1/>"), <foo></foo> will be replaced by <foo/> ("foo" characters are captured, and "$1" is replaced by them). <foo></bar> will also be replaced by <foo/>.
I hope that this explanation is enough for #Felix K. ;o)
(my English is not so good, that's why I did not provide many details)
if (someElement.innerText == string.Empty)
{
someElement.innerText = null;
}
I have a regular expression I'm using to remove html tags and now I'm wondering if there is any way to modify it so that it could also remove links beginning with http and ending with .stm or .gif?
This is the piece of code I'm using:
string BBCSplit = Regex.Replace(BBC, #"<(.|\n)*?>", string.Empty);
The best way to figure out regular expressions is through example, trial and error.
Put your html text in this site, along with your regexp and if it turns yellow, it's matched.
If you need a tutorial on how regexp works, I found this site to be very useful.
The regexp you'll want will be something like http:.*\.stm - which means "the characters http, followed by 0 or more characters (.*) followed by the characters .stm".