检索字符串正则表达式模式匹配

时间:2020-10-20 12:07:08

标签: python-3.x regex pattern-matching

我有一个列表#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* Insert(struct Node* head, int data) { struct Node* temp = (struct Node*) malloc(sizeof(struct Node)); temp->data = data; temp->next = NULL; //If the list is empty if (head == NULL) { head = temp; } else //The list is not empty { struct Node* temp1 = head; while (temp1->next != NULL) { temp1 = temp1->next; } temp1->next = temp; } return head; } void Print(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } struct Node* Reverse(struct Node* head) { struct Node* *prev, *current, *next; current = head; prev = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } head = prev; return head; } int main() { struct Node* head = NULL; printf("Enter the length of the linked list you want to create: "); int length; scanf("%d", &length); printf("Enter the value you want to input: "); int i; for (i = 0; i < length; i++) { int x; scanf("%d", &x); head = Insert(head, x); } printf("Given linked list\n"); Print(head); head = Reverse(head); printf("\nReversed linked list \n"); Print(head); return 0; }

input

如何从中提取以下字符串:

['ICE ERIE', 'ERIE', 'o9 ManGo', 'ManGo SLACKCURRAN 120mL', 'SLACKCURRAN']

另一个例子:

'ManGo SLACKCURRAN 120mL'

Input

['SWANSON', 'Apple Cider Vinegar Food Supplement Supplement mg per tablet DOUBLE STRENGTH FORMULA per tablet 1 NET', 'Cider', 'Vinegar', 'Food Supplement DOUBLE', 'Supplement', '200', 'per', 'tablet', 'DOUBLE', 'TABLETS 1 NET WEIGHT: 62g', '1', 'NET', 'WEIGHT:']

Output

我的尝试:

'TABLETS 1 NET WEIGHT: 62g' 

1 个答案:

答案 0 :(得分:1)

您可以使用

import re
input_l = ['ICE ERIE', 'ERIE', 'o9 ManGo', 'ManGo SLACKCURRAN 120mL', 'SLACKCURRAN']
reg = re.compile(r'\d*\.?\d+\s*(?:ounce|fl oz|foot|sq ft|pound|gram|inch|sq in|ml)\b', re.I)
print( list(filter(reg.search, input_l)) )
# => ['ManGo SLACKCURRAN 120mL']

请参见Python demo

注释

  • 使用re.search在字符串内的任何位置搜索匹配项(re.match仅在字符串开头搜索),请参见this thread
  • 删除^(字符串的开头)和$(字符串的结尾)锚点
  • 使用re.I标志进行不区分大小写的匹配
  • \d*\.?\d+是匹配整数或浮点数的更方便的模式,因为它还支持.95之类的数字
  • 以单词边界结束模式以匹配整个单词的度量单位(注意字符串文字前的r前缀)。
相关问题