官术网_书友最值得收藏!

What type of problems need regular expressions to solve

Some programmers wonder why they even need to learn regular expressions. Here are some use cases:

  • While searching for some text at times, there are cases where we don't know the value of the text upfront. We just know some rules or patterns of the text. For example, searching for a MAC address in a log message, searching for IP address in a web server access log, or searching for a 10-digit mobile number that may be optionally preceded by 0 or +<2 digit country code>.
  • Sometimes, the length of the text we are trying to extract is unknown, for example, searching URLs that start with http:// or https:// in a CSV file.
  • Sometimes, we need to split a given text on delimiters of a variable type and length and generate tokens.
  • Sometimes, we need to extract text that falls between two or more search patterns.
  • Often, we need to validate the various forms of user inputs, such as bank account number, passwords, usernames, credit card info, phone number, date of birth, and so on.
  • There are situations where you only want to capture all the repeated words from a line.
  • To convert input text into certain predefined formats, such as inserting a comma after every three digits or removing commas inside parentheses only.
  • To do a global search replace while skipping all the escaped characters.
主站蜘蛛池模板: 德清县| 白河县| 双桥区| 牡丹江市| 商洛市| 库伦旗| 新郑市| 东兴市| 大理市| 蓝山县| 栖霞市| 开化县| 江都市| 浑源县| 深州市| 同心县| 米林县| 荥经县| 江都市| 阿克陶县| 任丘市| 仁布县| 义乌市| 乡城县| 建阳市| 华宁县| 通化市| 化德县| 宣化县| 江源县| 高青县| 丰原市| 宿州市| 屯门区| 安远县| 岳池县| 花莲市| 兴化市| 韶关市| 石棉县| 凤庆县|