Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Regular Expressions

Regular Expressions

Scheduled Pinned Locked Moved C#
regexquestiontutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    the_jat
    wrote on last edited by
    #1

    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: True

    The 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: True

    The 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: True

    Above: 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: ,

    G 1 Reply Last reply
    0
    • T the_jat

      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: True

      The 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: True

      The 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: True

      Above: 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: ,

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Because you have specified that it should allow a zero count. The * is the same as {0,} which specifies that the pattern should match zero or more times.

      Despite everything, the person most likely to be fooling you next is yourself.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups