简体   繁体   中英

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. 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

Details :

  • (?m) Multi line
  • + 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] )
  • ? Matches between zero and one times
  • (?:) Non-capturing group
  • \\s Matches any whitespace character

Regex demo 1. Question

Regex demo 2. Question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM