match until a certain pattern using regex

后端 未结 3 1916
盖世英雄少女心
盖世英雄少女心 2020-12-09 12:31

I have string in a text file containing some text as follows:

txt = \"java.awt.GridBagLayout.layoutContainer\"

I am looking to get everyth

相关标签:
3条回答
  • 2020-12-09 13:12

    This seems to do what you want with re.findall(): (java\S?[^A-Z]*)\.[A-Z]

    0 讨论(0)
  • 2020-12-09 13:22

    Without using capture groups, you can use lookahead (the (?= ... ) business).

    java\s?[^A-Z]*(?=\.[A-Z]) should capture everything you're after. Here it is broken down:

    java            //Literal word "java"
    \s?             //Match for an optional space character. (can change to \s* if there can be multiple)
    [^A-Z]*         //Any number of non-capital-letter characters
    (?=\.[A-Z])     //Look ahead for (but don't add to selection) a literal period and a capital letter.
    
    0 讨论(0)
  • 2020-12-09 13:25

    Make your pattern match a period followed by a capital letter:

    '(java\S?[^A-Z]*?)\.[A-Z]'
    

    Everything in capture group one will be what you want.

    0 讨论(0)
提交回复
热议问题