正则表达式用于匹配多个字符串条件

huangapple go评论57阅读模式
英文:

Regex to match multiple string conditions

问题

以下是代码部分,不做翻译:

(?\<Group1\>(?:(.) ())|.*
(?\<Group1\>*.[a-zA-Z] )|(.*)

以下是翻译好的部分:

I have a need to capture two different string types returned by a query. The first string has data that needs to be trimmed off while the second string is just text.

Review / Sign Changes
City & State for Current Visit

I tried

(?\<Group1\>(?:(.) ())|.*
(?\<Group1\>*.[a-zA-Z] )|(.*)

I expected:

Review / Sign Changes
City & State for Current Visit

I'm not strong in Regex but I try :) Any help would be appreciated.
英文:

I have a need to capture two different string types returned by a query. The first string has data that needs to be trimmed off while the second string is just text.

Review / Sign Changes  (Doe,John Howard - 555-00-5555)
City & State for Current Visit

I tried

(?\<Group1\>(?:(.)  ())|.\*
(?\<Group1\>\*.\[a-zA-Z\] )|(.\*)

I expected:

Review / Sign Changes
City & State for Current Visit

I'm not strong in Regex but I try 正则表达式用于匹配多个字符串条件 Any help would be appreciated.

答案1

得分: 2

这个正则表达式:

^[^(]+

正则表达式的匹配如下:

节点 解释
^ 字符串的开头
[^(]+ 除了 ( 之外的任何字符(1次或多次(尽可能匹配最多的字符))
英文:

This regex:

^[^(]+

Online demo

The regular expression matches as follows:

Node Explanation
^ the beginning of the string
[^(]+ any character except: ( (1 or more times (matching the most amount possible))

答案2

得分: 1

如果第一个字符串的格式保持一致,您可以跳过正则表达式,只需按两个空格拆分字符串并取第一部分。对于第二个字符串,我看不到任何更改,所以您不需要使用正则表达式。

英文:

If the formatting of the first string is consistent, you can skip regex and just split the string by two spaces and take the first part. For the second string, I see no changes at all so you shouldn't need a regex there.

huangapple
  • 本文由 发表于 2023年2月16日 02:11:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定