简体   繁体   English

如何使用正则表达式匹配和复制所有字符,直到首次出现一个字符?

[英]How to match and copy everything up to first occurrence of a character using regex?

Sample text format: 示例文本格式:

tom@yahoo.com,age:30|http://....
tom1@yahoo.com,age:31|http://....
tom2@yahoo.com,age:32|http://....

I want to match and copy everything before the '|'. 我想匹配并复制'|'之前的所有内容。 My regex: ^[^|]+ this will match everything up to the '|', how can I copy all the occurrences? 我的正则表达式: ^[^|]+这将匹配所有'|',我该如何复制所有出现的内容?

2nd question Sample text format: 第二个问题示例文本格式:

tom@yahoo.com,age:30|100 lb
tom1@yahoo.com,age:31|200 lb
tom2@yahoo.com,age:32|300 lb

how can I match and copy all the text and range of 200 lb and below. 如何匹配和复制200磅及以下的所有文本和范围。 So the first two line should be extracted and copied. 因此,应该提取并复制前两行。

Regex : 正则表达式

  1. (?m)^[^|]+

  2. (?m)^[^|]+\\|\\b(?:[1]?[0-9][0-9]?|200)\\slb\\b or (?m)^[^|]+\\|\\b(?:1?\\d\\d?|200)\\slb\\b (?m)^[^|]+\\|\\b(?:[1]?[0-9][0-9]?|200)\\slb\\b(?m)^[^|]+\\|\\b(?:1?\\d\\d?|200)\\slb\\b

Details : 详细资料

  • (?m) Multi line (?m)多行
  • + Matches between one and unlimited times +无限次匹配
  • [] Match a single character present in the list []匹配列表中存在的单个字符
  • [^] Match a single character not present in the list [^]匹配列表中不存在的单个字符
  • | or 要么
  • \\d matches a digit (equal to [0-9] ) \\d匹配一个数字(等于[0-9]
  • ? Matches between zero and one times 零到一匹配
  • (?:) Non-capturing group (?:)非捕获组
  • \\s Matches any whitespace character \\s匹配任何空格字符

Regex demo 1. Question 正则表达式演示1.问题

Regex demo 2. Question 正则表达式演示2.问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所有内容匹配到字符的第二次出现? - How to match everything up to the second occurrence of a character? 正则表达式,如何匹配所有出现的事件 - Regex, how to match everything up to nth occurrence 如何使用正则表达式在第一次出现 3 到 18 位数字之前匹配所有内容? - How to match everything before the first occurrence of a 3 to 18 digit number using Regex? 如何使用正则表达式仅匹配首次出现 - How to match only first occurrence using Regex 正则表达式:一个字符的最后一次出现和另一个字符的第一次出现之间的匹配 - Regex: match between last occurrence of a character and first occurrence of another character 匹配正则表达式直到第一次出现特殊字符'->' - Match regex until the first occurrence of special character '->' ruby 正则表达式 - 如何匹配直到字符的所有内容 - - ruby regex - how to match everything up till the character - 正则表达式:匹配第一次出现的带有字符 'a' 的单词 - Regex: matching up to the first occurrence of word with character 'a' in it 正则表达式:匹配字符的第一次出现 - Regex: matching up to the first occurrence of a character 正则表达式匹配字符串的第一部分(直到第一次出现空格字符),如果它不包含序列 ;host= - Regex to match first part of string (up to the first occurrence of a space character) if it doesn't contain the sequence ;host=
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM