Regular Expressions
-
Hi! everyone, Regular Expression sometimes match empty strings, can someone tell me why it happens and what is the use of matching empty strings(why have the original designers made it that way)? Here are some examples: In these examples, the "Pattern" is the Pattern used for matching and the "MatchWith" is the string that is evaluated. The "Count" is the number of hits, and finally "Occurrences" lists all of them. Following is the output of a console application.
Pattern: \d*
MatchWith: 34544
String: 34544, Pattern: \d*, Match: |TRUE|
M String: 34544, Position: 0, Count: 2, S Length: 5
Occurences:
String: 34544, Length: 5, Position: 0, Success: True
String: , Length: 0, Position: 5, Success: TrueThe above example matches an empty string, why?
Pattern: \d*
MatchWith: dfgdr
String: dfgdr, Pattern: \d*, Match: |TRUE|
M String: , Position: 0, Count: 6, S Length: 5
Occurences:
String: , Length: 0, Position: 0, Success: True
String: , Length: 0, Position: 1, Success: True
String: , Length: 0, Position: 2, Success: True
String: , Length: 0, Position: 3, Success: True
String: , Length: 0, Position: 4, Success: True
String: , Length: 0, Position: 5, Success: TrueThe above example matches 6 empty strings, more than the actual length of the input string, why? Following are examples of Lookahead:
Pattern: (?=.*\d)
MatchWith: 44
String: 44, Pattern: (?=.*\d), Match: |TRUE|
M String: , Position: 0, Count: 2, S Length: 2
Occurences:
String: , Length: 0, Position: 0, Success: True
String: , Length: 0, Position: 1, Success: TrueAbove: again an empty string.
Pattern: (?=.*\d)
MatchWith: dfg
String: dfg, Pattern: (?=.*\d), Match: |FALSE|
M String: , Position: 0, Count: 0, S Length: 3
Occurences:Above: same failed with strings as input: "FALSE"
Pattern: (?=.*\d)
MatchWith: fgd3
String: fgd3, Pattern: (?=.*\d), Match: |TRUE|
M String: , Position: 0, Count: 4, S Length: 4
Occurences:
String: , -
Hi! everyone, Regular Expression sometimes match empty strings, can someone tell me why it happens and what is the use of matching empty strings(why have the original designers made it that way)? Here are some examples: In these examples, the "Pattern" is the Pattern used for matching and the "MatchWith" is the string that is evaluated. The "Count" is the number of hits, and finally "Occurrences" lists all of them. Following is the output of a console application.
Pattern: \d*
MatchWith: 34544
String: 34544, Pattern: \d*, Match: |TRUE|
M String: 34544, Position: 0, Count: 2, S Length: 5
Occurences:
String: 34544, Length: 5, Position: 0, Success: True
String: , Length: 0, Position: 5, Success: TrueThe above example matches an empty string, why?
Pattern: \d*
MatchWith: dfgdr
String: dfgdr, Pattern: \d*, Match: |TRUE|
M String: , Position: 0, Count: 6, S Length: 5
Occurences:
String: , Length: 0, Position: 0, Success: True
String: , Length: 0, Position: 1, Success: True
String: , Length: 0, Position: 2, Success: True
String: , Length: 0, Position: 3, Success: True
String: , Length: 0, Position: 4, Success: True
String: , Length: 0, Position: 5, Success: TrueThe above example matches 6 empty strings, more than the actual length of the input string, why? Following are examples of Lookahead:
Pattern: (?=.*\d)
MatchWith: 44
String: 44, Pattern: (?=.*\d), Match: |TRUE|
M String: , Position: 0, Count: 2, S Length: 2
Occurences:
String: , Length: 0, Position: 0, Success: True
String: , Length: 0, Position: 1, Success: TrueAbove: again an empty string.
Pattern: (?=.*\d)
MatchWith: dfg
String: dfg, Pattern: (?=.*\d), Match: |FALSE|
M String: , Position: 0, Count: 0, S Length: 3
Occurences:Above: same failed with strings as input: "FALSE"
Pattern: (?=.*\d)
MatchWith: fgd3
String: fgd3, Pattern: (?=.*\d), Match: |TRUE|
M String: , Position: 0, Count: 4, S Length: 4
Occurences:
String: ,