Regular Expression application for string cutting in C #

Source: Internet
Author: User

This is part of the code I used to cut strings using regular expressions:

Function Description: extract multiple types of data details from a Logistics Information string

The data to be cut out includes:

Starting Place, destination, cargo quantity, cargo unit, cargo type, vehicle quantity, vehicle length, vehicle type, price, price unit

Examples of string types that need to be cut (multiple types, including car-seeking and goods-seeking ):

 

1. Hunan Hengyang-> Hunan Xiangtan, Hunan Changsha, with 6-meter semi-closed vehicles, 1-8 tons of goods

2. Changsha, Hunan> Nanyang, Henan Province, with 9.6-meter flatbed trucks for goods

3. Changsha, Hunan-> Qinghai, Xi'an, Shaanxi, Hanzhong, and Ankang, Shaanxi. There are two 6.8-meter high-column vehicles, which are 12-25 tons of goods.

4. Hunan Xiangtan-> Hunan Yueyang linxiang, with 9.6-17.5 tons of heavy cargo, please get 1-3-meters of vehicles. Today, the car will be loaded tomorrow

5. Hunan Yueyang linxiang> Heze, Shandong Province, has 15-28 tons of cotton. The price is 9.6-17 meters, and the price is 230 yuan/ton. goods can be loaded immediately.

6. Changsha, Hunan-> Anhua County, Yiyang, Hunan province, has 1 ton of goods. 2-3 meters of vehicles are required for urgent transportation.

7. Hunan Xiangtan-> Ganzhou, Jiangxi Province, with 35-40 tons of goods, seeking for a semi-trailer and urgent operation

...

 

Of course, these are just examples.

The detailed code for the method is as follows:

Using System. Text;

Using System. Text. Regularexpressions;

# Region uses regular expressions to cut strings
/// <Summary>
/// The attributes assigned to the KC_INFO instance by the cut string:
/// KCI_START_ADDRESS, KCI_END_ADDRESS, KCP_GOODS_ID, KCI_GOODS_NUMBER, Baidu, KCP_CAR_ID, KCI_CAR_NUMBER, KCI_CAR_LENGTH, KCI_PRICE, KCI_PRICE_UNIT
/// </Summary>
/// <Param name = "ki"> </param>
Public void splitMainInfoString (KC_INFO ki)
{
String reg_start_address = string. Format (@ "^ [^->] * (? =-> )");
String reg_end_address = string. Format (@"(? <=->) [^,] * (? = ,)");
String reg_goods_number = string. Empty;
String reg_goods_unit = string. Empty;
String reg_goods_id = string. Empty;
String reg_car_id = string. Empty;
String reg_car_number = string. Empty;
String reg_car_length = string. Empty;
String reg_price = string. Empty;
String reg_price_unit = string. Empty;
// Use commas (,) to separate strings
MatchCollection MS = Regex. Matches (InfoText, @ "[^,] * [^ s] (? =, | $ )");
String goodsInfoString = string. Empty;
String carInfoString = string. Empty;
If (new int [] {2, 22, 5}. Contains (this. Type) // get the car in stock
{
// Cargo information string
GoodsInfoString = MS [1]. ToString ();
// Vehicle Information string
CarInfoString = MS [2]. ToString ();
// Regularexpression matching the number of goods
Reg_goods_number = string. Format (@"(? <= Yes) (d +. d +) | (d +-d +) | d + )(? = (Tons | square | parts | car | kg | items | units ))");
// Goods matching unit
Reg_goods_unit = string. Format (@"(? <= Yes (d + .? D +) | (d +-d +) | d +) (tons | square | parts | car | kg | units )");
// Goods matching unit
Reg_goods_id = string. Format (@"(? <= Yes (d +. d) | (d +-d +) (tons | square | parts | car | kg | units) w + ");

 

// Match the number of vehicles required
Reg_car_number = string. Format (@"(? <= Evaluate) (d +-d +) | (d + ))(? = Sets) | (? <=) A large number ");
// Match the length of the vehicle
Reg_car_length = @"(? <= Evaluate * [^ d]) (d +. d +) | (d +-d +) | (d + .? D {0,}-d + .? D {0 ,}) | (d + ))(? = Meters )";
// Match the vehicle type
Reg_car_id = string. Format (@ "(refrigerating | the last eight wheels or the last four or the last eight | zero-slice | s cart-free | coach | barge | 60 public Column half hanging )(? = Car )");
}
Else // get the goods by car
{
GoodsInfoString = MS [2]. ToString ();
CarInfoString = MS [1]. ToString ();
// Match the quantity of required goods
Reg_goods_number = @"(? <= Evaluate) (d + .? D {0 ,})-(d + .? D {0 ,}) | (d +. d +) | (d +) | large number )";
// The cargo unit is ton.
Reg_goods_unit = string. Format ("ton ");
// There are no restrictions on cargo types
Reg_goods_id = string. Empty;
// Number of matched vehicles
Reg_car_number = string. Format (@"(? <= Yes) (d +-d + | a large number )(? = Sets )");
// Match the length of the vehicle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.