Home > Back-end >  Turn a regular problem, look be like simple, really didn't want to come out
Turn a regular problem, look be like simple, really didn't want to come out

Time:10-08

Glass/HD/Sc1/Good/ABC/Z/111

Such a character, to match the first n "/" and "/" before the string of words, how to write a regular?
The first one is Glass, for example, the fourth is Good

CodePudding user response:

RegExp:
 ^ (+ \ [^ \]//) {4} 


Change the number is corresponding to the n

Match the following figure, can enlarge the:

CodePudding user response:

I don't think it's necessary to use a regular, string a good
For example,
String s="Glass/HD/Sc1/Good/ABC/Z/111";
String reg="/\ \ s \ \ s * *";
System. The out. Printf (" % s, % s \ n ", s.s plit (reg) [0], s.s plit (reg) [3]).//s divided into an array, the first and fourth elements, the array subscript starting from 0

CodePudding user response:

Write a complete answer, again, a total of three schemes, scheme 1 I answer above supplement for,

===============================
Compatible with all regular engine plan 1:
 ^ (? : + \ [^ \]//) {0} (/[^ \] +) 
^ (? : + \ [^ \]//) {1} (/[^ \] +)
^ (? : + \ [^ \]//) {3} (/[^ \] +)
^ (? : + \ [^ \]//) {5} (/[^ \] +)
^ (? : + \ [^ \]//) {6} (/[^ \] +)
^ (? : + \ [^ \]//) {7} (/[^ \] +)


Numbers within the curly braces, n - 1 the rightmost parentheses matching content will be stored in group 1, so you can use the code to obtain the value of group 1, group green part of the text on the value of the content as shown in figure 1:

===============================
If your engine support, you can use the solution 2:

 ^ (+ \ [^ \]//) {} ((* SKIP)? !). |/[^ \] + 
^ (+ \ [^ \]//) {1} ((* SKIP)? !). |/[^ \] +
^ (+ \ [^ \]//) {3} ((* SKIP)? !). |/[^ \] +


More than 3 regular respectively part 1, part 2, part 4,

The Numbers to the left to skip number, such as "XXX/" paragraph
If don't need to jump out of date is left blank, such as:
 ^ (+ \ [^ \]//) {} ((* SKIP)? !). |/[^ \] + 


Is equal to:
 [^ \/] + 


Match the figure is as follows:

===============================
Finally all engine scheme is to support 3:

This is interesting, can reverse first string, matching and reverse back home in the end, the regular is also written,

Regular is as follows:
/[^ \] + (?=(? : \ [^ \/] +) {0} $) 
[^ \]/+ (?=(? : \ [^ \/] +) {1} $)
[^ \]/+ (?=(? : \ [^ \/] +) {3} $)
[^ \]/+ (?=(? : \ [^ \/] +) {5} $)
[^ \]/+ (?=(? : \ [^ \/] +) {6} $)
[^ \]/+ (?=(? : \ [^ \/] +) {7} $)


Which number is said to skip the left side of the original string "XXX/" the number of pieces

The matching results as shown in figure:


This scheme is suitable for alternative content, more than just the reverse process, but the best compatibility, all engine support, even more complex also allow you to extend the demand, just don't forget to regular in string content after the reverse order to write,

Finally, JavaScript, and Python and vba (vb6) code to run the Demo as end (Java I wasn't running environment, so can't demonstrated) :

CodePudding user response:

There is a the most easy to think of, but the worst compatibility, highly stable solution 4:

The plan many engines do not support, but because suddenly found the latest version of Chrome unexpectedly to JS support the regular function, so write it out,

Regular code for the following:
 (? <=^ (? : \ [^ \]/+/)/[^ \] + {0}) 
(? <=^ (? : + \ [^ \]///[^ \]) {3}) +


The digital meaning of n - 1,

Regular engine from left to right order primarily, so generally support even if is simple, fixed the look - behind the behavior of the length of the string, this example look - behind the part of the regular length is unknown,

Let's look at the following with the latest version of Chrome test results:



As you can see, although the new Chrome JS has supported as a regular function (also support seems to be the.net), but the effect is very poor, for the first time to build the regular costs have nearly four minutes, if in the program, this card's effect but fatal disaster, so don't recommend using this scheme, other several common language I don't support also tested, and Java me not environment temporarily not testing, but consider this so many decades of Java virtual machine, it is conceivable - behind undetermined length look regular behavior is a big probability have no stability,
Give up this plan,

  • Related