-4

So I have a case where i want to get every 5 character code which is delimited by / Problem is the codes itself can contain the delimiter so I can't use something like ([^\/]+)

E.g.

  • 07070 should give me 07070
  • 07070/01010 should give me 07070 01010
  • 0/0/0/BBBBB should give me 0/0/0 BBBBB
  • AAAAA/BBBBB/CCCCC should give me AAAAA BBBBB CCCCCC

Any ideas how i can use a regex to achieve this?

Toto
  • 83,193
  • 59
  • 77
  • 109

3 Answers3

0
use : (?<yourToken>.{5})\/?

yourToken : is the name of match group you want. while iterating on your code get match group with name yourToken

Note that : 07/01010 will get 07/01 only

in java :

Pattern pattern = Pattern.compile("(?<yourToken>.{5})\\/?");
Matcher matcher = pattern.matcher("AAAAA/BBBBB/CCCCC");
while (matcher.find()){
  System.out.println(matcher.group("yourToken"));
}

check here: https://regex101.com/r/4vBAL9/1

0

Since you know your codes are always 5 chars followed by a delimiting / you can just count 5 chars:

(.{5})(\/?)

https://regex101.com/r/paLxPv/1

Your codes will be in \1

MonkeyZeus
  • 18,445
  • 3
  • 30
  • 67
0
  • Find: /(?=[^/]{5}(?:/| |$))
  • Replace:      # (a space)

Demo & explanation

Toto
  • 83,193
  • 59
  • 77
  • 109