Home > Net >  how to catch regex a special tag
how to catch regex a special tag

Time:12-16

I am trying to catch a series of tags with regex:

/<shi>(\n|<shi\n>/

How do I match the cases below:

  • <shi>
  • <\nshi>
  • <\nshi\n>
  • <shi\n\n>
    ... (And more cases like that)

CodePudding user response:

/<\n*shi\n*>/ should do the job. Let's see why:

  • < will match the character < once
  • \n* will match the newline character between 0 and unlimited times (greedy)
  • shi will match the characters shi literally (case sensitive)
  • \n* will match the newline character between 0 and unlimited times (greedy)
  • > will match the character > once

@NNL993's answer (<(\n )?shi(\n )?>) also works, but it uses capturing groups.

  • \n will match the newline character
  • will match the previous token (newline character) between one and unlimited times (greedy)
  • ? will match the previous token ((\n ) group) between zero and one time
  • Related