Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

Regex match character with negative lookbehind of two chars

I want to split a string using the character "/", but the split should only occur if there is no "" in front of it.

String:

/10/102-/ABC083.013/11/201201/20/83/30/463098194/32/7.7/40/0:20

Regex:

/*(?<!\)[^/]*/*(?<!\)[^/]*

Expected result:

/10/102-/ABC083.013
/11/201201
/20/83
/30/463098194
/32/7.7
/40/0:20

But with my regex I get:

/10/102-
/ABC083.013/11
/201201/20
/83/30
/463098194/32
/7.7/40
/0:20

online regex example

The issue is on the first group "/10/102-/ABC083.013", it does not recognize the string "/" to the first group. I don't know how to optimize/change my regex so that it recognizes the first group correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use

(?:/[^\/]+){2}(?:\/[^\/]+)?

See the regex demo. Details:

  • (?:/[^\/]+){2} - two occurrences of
    • / - a / char
    • [^\/]+ - one or more chars other than / and
  • (?:\/[^\/]+)? - an optional occurrence of:
    • \ - a char
    • / - a / char
    • [^\/]+ - one or more chars other than / and

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...