Regex to match only uppercase “words” with some exceptions

前端 未结 6 2042
面向向阳花
面向向阳花 2020-12-02 20:26

I have technical strings as the following:

\"The thing P1 must connect to the J236 thing in the Foo position.\"

I would like to match with

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 21:10

    Maybe you can run this regex first to see if the line is all caps:

    ^[A-Z \d\W]+$
    

    That will match only if it's a line like THING P1 MUST CONNECT TO X2.

    Otherwise, you should be able to pull out the individual uppercase phrases with this:

    [A-Z][A-Z\d]+
    

    That should match "P1" and "J236" in The thing P1 must connect to the J236 thing in the Foo position.

提交回复
热议问题