Here are two example strings:
'If <Person>, is for any reason unwilling or unable to serve, <next.Person > shall instead serve as successor agent.'
'If <Person>, is for any reason unwilling or unable to serve, <next.Person> shall instead serve as successor agent.'
I'm looking to match the second, and not the first, there can be no whitespaces inside the <>
's. I've tried several answers on SO, negative lookbehind will not work because the chars inside the <>
are not fixed width.
I'm looking for a pattern that would match everything inside the ''
's when none of the <>
sections contain a space (\s
to be regex-specific). As shown in the example above there can be multiple <>
's inside the string, and the string can contain pretty much any valid characters outside of the pattern I wish to exclude.
CodePudding user response:
You can use
^(?:<[^\s<>]*>|[^<>])*$
See the regex demo.
Details:
^
- start of string(?:<[^\s<>]*>|[^<>])*
- zero or more occurrences of<[^\s<>]*>
- a<
, zero or more chars other than<
,>
and whitespace and then a>
char|
- or[^<>]
- a char other than<
and>
$
- end of string.