Home > Software design >  Regular expression to match strings between the two same characters from a selection of different ch
Regular expression to match strings between the two same characters from a selection of different ch

Time:06-07

I'm looking for a regular expression to match every characters that are between two a or two b but not between an a and a b.

For example:

accccca → should match
bfffffb → should match
azzzzzb → shouldn't match
bttttta → shouldn't match

ammbmma → should match from the a to the a
bllalla → should match only between the two a

How can I do that?

CodePudding user response:

I think something like a. a|b. b should work.

. means at least one character, so a. a would match an a, at least one but up to infinity other characters, followed by another a. b. b works similarly, and then | works to OR the the two expressions together.

CodePudding user response:

Try this pattern: (a|b)(.*)\1

See Regex Demo

Explanation

  • (...) A captured group (group 1)
  • a|b Match with a or b.
  • (.*) Match with any character zero or more times and capture this as group 2
  • \1 Match with the captured group, if the group captured "a" the \1 means "a". basically, it means what you match in start using that in this place.

CodePudding user response:

You could use

(([ab]).*?\2)
  • ( Capturing group
    • ( Capturing group
      • [ab] Match a or b character
    • ) Close group
    • .*?\2 Non-greedy match till the second group
  • ) Close group

See the demo

CodePudding user response:

As you do not want to capture a and b markers themself, you can use a positive lookbehind and lookahead.

As marker should be the same at the end as at the start you can use a capturing group in a lookaround.

So regex would be like this

(?<=([ab]))(.*)(?=\1)

Please, check a demo https://regex101.com/r/ebZRDz/3

  • Related