- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Reply to: Losing the plot on C# Regex
Collapse
You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:
- You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
- You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
- If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.
Logging in...
Previously on "Losing the plot on C# Regex"
Collapse
-
But does SFA for contract security!Originally posted by DimPrawnWhat is this fascination with writing obscure, arcane, unreadable, unsupportable, unextendable, unfixable code?
For me, the genius is the man that can take a problem and express the solution in the simplest and most understandable way that makes extending the idea fast (and therefore cheap).
Leave a comment:
-
What is this fascination with writing obscure, arcane, unreadable, unsupportable, unextendable, unfixable code?
For me, the genius is the man that can take a problem and express the solution in the simplest and most understandable way that makes extending the idea fast (and therefore cheap).
Leave a comment:
-
Originally posted by TheMonkeyOr postscript as well. The following program runs in a postscript and perl interpreter:
http://perl.plover.com/obfuscated/bestever.pl
Insane!
Postscript is a language best left to the masochistic or the insane
Leave a comment:
-
Or postscript as well. The following program runs in a postscript and perl interpreter:Originally posted by DaveBOr a Perl Programmer, for when you really want to confuse people
http://perl.plover.com/obfuscated/bestever.pl
Insane!
Leave a comment:
-
Originally posted by Joe BlackIndeed, and I'll second that, regex is definately a black art which you'd have to be a C programmer to fully understand.
Or a Perl Programmer, for when you really want to confuse people
Leave a comment:
-
I'm a C guy too! I think there may be a pattern you have discovered. Something to do with obfuscatable languages... Hmm I'm also a Perl guy. This gets deeper.Originally posted by Joe BlackIndeed, and I'll second that, regex is definately a black art which you'd have to be a C programmer to fully understand.
Leave a comment:
-
Indeed, and I'll second that, regex is definately a black art which you'd have to be a C programmer to fully understand.Originally posted by scotspinethat's what i was suggesting. mercifully, life is far too short to spend time on the black art of reg exps...
Leave a comment:
-
Indeed - which is a good thing because I get paid a lot to maintain them after I've written themOriginally posted by scotspinethat's what i was suggesting. mercifully, life is far too short to spend time on the black art of reg exps...
Leave a comment:
-
that's what i was suggesting. mercifully, life is far too short to spend time on the black art of reg exps...
Leave a comment:
-
Solved - I think!
Looks like I might have it by doing it in two chunks.
Find all links in HTML:
Then stick the linkhtml group into a 2nd regex:PHP Code:_regex = new Regex(@"(?<linkhtml><a[\s]+[^>]*?href[\s]?=[\s\""\']+(?<href>.*?)[\""\']+.*?>)(?<linktext>[^<]+|.*?)?<\/a>", RegexOptions.IgnoreCase | RegexOptions.Compiled |RegexOptions.Multiline);
To get all the attributes as name and value pairs.PHP Code:_regxAttributes = new Regex("(?<name>\\b\\w+\\b)\\s*=\\s*(\"(?<value>[^\"]*)\"|'(?<value>[^']*)'|(?<value>\" + [^\"\"'<> \\s]+)\\s*)+",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
If anyone knows how to capture every hyperlink in the HTML and capture all the attributes in one step I'd be pleased to know.
Cheers,
DP
Leave a comment:
-
I'm trying to match and capture all the main link attributes, href, title, rel, class in any order in any combination along with any other valid HTML attribute.
Any ideas?
Leave a comment:
-
this works
Regex regex = new Regex(@"(<a\s+)*title\s*=\s*(?:""|')(?<title>[^'""]*)(?:""|')", RegexOptions.IgnoreCase);
Leave a comment:
-
Losing the plot on C# Regex
Hi peeps,
I wonder if any of you .NET guys could try and figure out why my regex works in several tools, but when put into C# it fails?
Using RegExBuddy, Rad software Regular Expression Designer, Sells Brothers RegExDesigner.NET I can match the "title" group, but my C# fails to (although it gets the "href".PHP Code:string input = @"<a href=""http://www.thisisthedomain.com/"" class='big one' title=""testing & fixing & failing"">text here - More Text - some more</a>";
// Parse HTML for other attributes
Regex regex = new Regex(@"(<a\s+)*title\s*=\s*(?:""|')(?<title>[^'""]*)(?:""|')|class\s*=\s*(?:""|')(?<class>[^'""]*)(?:""|')|href\s*=\s*(?:""|')(?<href>[^'""]*)(?:""|')|rel\s*=\s*(?:""|')(?<rel>[^'""]*)(?:""|')", RegexOptions.IgnoreCase);
Match match = _regex.Match(input);
// Fetch named groups
_rel = match.Groups["rel"].Value;
_href = match.Groups["href"].Value;
_title = match.Groups["title"].Value;
_class = match.Groups["class"].Value;
Using 1.1 of the Framework.
If anyone can see what's going wrong I'd be really chuffed.
Cheers,
DPTags: None
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers
Contractor Services
CUK News
- How salary sacrifice pension changes will hit contractors Today 07:48
- All the big IR35/employment status cases of 2025: ranked Yesterday 08:55
- Why IT contractors are (understandably) fed up with recruitment agencies Dec 22 13:57
- Contractors, don’t fall foul of HMRC’s expenses rules this Christmas party season Dec 19 09:55
- A delay to the employment status consultation isn’t why an IR35 fix looks further out of reach Dec 18 08:22
- How asking a tech jobs agency basic questions got one IT contractor withdrawn Dec 17 07:21
- Are Home Office immigration policies sacrificing IT contractors for ‘cheap labour’? Dec 16 07:48
- Will 2026 see the return of the ‘Outside IR35’ contractor? Dec 15 07:51
- Contractors, Reeves’ dividends raid is disastrous. Act, but without acceptance Dec 12 07:10
- Why JSL indemnity clauses putting umbrella contractors on the hook could be a PR disaster Dec 11 07:36

Leave a comment: