简体   繁体   English

没有单词匹配的正则表达式

[英]regular expression without word match

I have this expression: 我有这个表达:

-a bp cd 4 -6 3 -n sig3 -p 0.5 0.7

I want to match all from -a to -n . 我想将所有-a匹配到-n This means that the prefix and suffix are: - with one letter. 这意味着前缀和后缀为: -带有一个字母。

I have a start with: (?<=-a )(?<ida>[^-]*) 我开始于: (?<=-a )(?<ida>[^-]*)

But i need to exclude also the letter. 但我还需要排除这封信。

Note that the -n can be any other letter with - before it and know at run time only. 请注意,-n可以是-之前的任何其他字母,并且仅在运行时知道。

How should I do that? 我应该怎么做?

Thanks to all the Answers, i finally cracked it: 感谢所有的答案,我终于破解了:
(?<=-a )(?.*?(?=-[az]|$)) (?<=-a)(?。*?(?=-[[az] | $)))

You can do it like this: 您可以这样做:

(?<=-a )(?>[^ ]+| (?!-n))*(?= -n)

The interest of this expression is to avoid lazy quantifiers and to trim spaces at the begining and at the end. 此表达式的目的是避免延迟量词,并在开头和结尾处修剪空格。

Details: 细节:

(?<=-a )          # preceded by `-a `
(?>               # open an atomic group (non-capturing)
    [^ ]+         # all that is not a space
  |               # or
    [ ](?!-n)     # a space not followed by `-n`
)*                # repeat the group zero or more times
(?= -n)           # check if ` -n` follows

If I understood you correctly, your pattern is: 如果我对您的理解正确,则您的模式是:

  • '-a' followed by '-a'后跟
  • anything that is not a dash [^-] or a dash followed by a character that is not a letter -[^az] repeated one or more times, followed by 任何不是破折号[^-]或破折号后跟不是字母的字符-[^az]重复一次或多次,然后再跟
  • a dash and a letter -[az] 破折号和字母-[az]

This regex should do it: 这个正则表达式应该做到这一点:

(?<=-a)([^-]|-[^a-z])+(?=-[a-z])

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

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