VS Regular Expression Guidelines

I was wondering if there are general guidelines for using regex VS "string".contains("anotherString")and / or other String API calls?

While the above solution is .contains()trivial (why bother with regex if you can do it at a time), real life brings more complex solutions. For example, is it better to make two calls .contains()or one regular expression?

My rule was to always use a regular expression, unless it could be replaced with a single API call. This prevents code from sprawling, but it's probably not that good in terms of code readability, especially if the regex tends to get big.

Another often overlooked argument is performance. How do you know how many iterations (as in "Big O") are required for this regular expression? Will it be faster than a simple repetition? For some reason, everyone suggests that if the regular expression looks shorter than 5 if, it should be faster. But is this always the case? This is especially true if the regular expression cannot be precompiled in advance.

+3
source share
4 answers

The answer (as usual) is that it depends.

, , "this | that", find. . "OR" , , . ( ):

for( i = 0; i < stringLength; i++ ) {
    if( stringAt pos i starts with "this" )
       found!
    if( stringAt pos i starts with "that" )
       found!
}

. () .

, : ".*this.*|.*that.*" .

, , , . , , contains. , "A" "B" "g" - "m"... regex.

.

-1

, , , /. .contains() , , , .

, . .contains() ( ) , .

: , .

+3

RegexBuddy . , , . , ( O) . "" RegexBuddy, , .

, . , , , .

5 if , regex one|two|three|four|five , , o, t f. 5, , , , 5 , . five, , if if , 5- .

+3

Source: https://habr.com/ru/post/1723868/


All Articles