Previously, I introduced the usage, the pros and cons of Regular Expressions in extracting HTML content.
Using Regular Expression to Match HTML
Advanced Text â Recommendations to Handle HTML With Regular Expression
Regular expressions can be used:
- Data validation. (A string complies with email rule or is a phone number, etc.)
- Replace text. (Replace the characters we donât want with other characters.)
- Extract substrings from a string based on a pattern match. (Search specific text in the document.)
In data processing, we mainly use Regular Expressions to replace text and extract substrings.
Examples:
Remove all non-alphanumeric characters other than invalid characters (@-.) from the string, then return a string.
Sample code (C#)
using System;
using System.Text.RegularExpressions;
void Main()
{
    Console.WriteLine(CleanInput(â@octoparse*.123#facebook$%-JSâ));
}
static string CleanInput(string strIn)
{
   // Replace invalid characters with empty strings.
   return Regex.Replace(strIn, @â[^\w\.@-]â, ââ);
}
The output
@octoparse.123facebook-JSÂ
We can see the characters (* # $ %)Â which we believe to be invalid characters are replaced by an empty string, which removed these invalid characters
Examples:
get the domain name of URL
Sample code (C#)
using System;
using System.Text.RegularExpressions;
void Main()
{
    string url = âhttp://www.octoparse.com/pricing/â;
    Regex r = new Regex(@â((\w)+\.)+\w+â,RegexOptions.IgnorePatternWhitespace);
    Console.WriteLine(r.Match(url).Captures[0].Value);
}
The output
www.octoparse.com
From the code above, we see that Regular Expression can help you extract strings with characteristics of domain names from the URL.
For hundreds of thousands of URL like this, you can extract their domain names with only a regular expression.