How to match the coupled string with regular expression?
I want to match this kind of format:
AA sysodufsoufdds AA
Where AA can be arbitary consecutive string with no spac开发者_如何学Goe in it.
Is there a solution?
How about this:
^(\w+).*?\1$
This will match any char sequence followed by anything followed by same char seq at the front. So it'll match:
AA sysodufsoufdds AA BBB sysodufsoufdds BBB ABC sysodufsoufdds ABC
How about
AA.*?AA
or to match whole string
^AA.*?AA$
This matches a chunk of characters followed by a space followed by anything followed a space followed by the first chunk of characters...
([A-Z0-9]+) .* \1
There are a number of different ways of matching these bits. The key thing is the use of \1
, which is a backrefrence to the first defined pattern. If you had two patterns you could use \2
to refer to the second one. For instance this ...
([A-Z0-9]+) (.*) \1 \2
... would match this string
AA sysodufsoufdds AA sysodufsoufdds
精彩评论