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. Regular Expressions
  4. Regex to match only first 5 positions of a line of text (and nothing more)

Regex to match only first 5 positions of a line of text (and nothing more)

Scheduled Pinned Locked Moved Regular Expressions
regexquestioncsharp
1 Posts 1 Posters 1 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.
  • L Offline
    L Offline
    lewist57
    wrote on last edited by
    #1

    Want regex to match the following pattern in the first 5 positions (only) of a line of text: position 1............2..............3..............4..............5 s........s or d.....s or d.....s or d.....s or d where s = white space, n = digit The first position is always a white space, and the next 4 positions can be one or more digits. examples: s1234 ss123 sss12 ssss1 s1sss s12ss s123s I tried ^\s+\d+ In an online regex tester, this matches the digits perfectly in the first 5 positions and not any where else on the line. However in a C# program, it is matching not only what is in the first 5 positions of the line, but also any other combinations of s and d on the same line, such as good.......................bad............................bad match.....................match.......................match 12 text text text 53, text text text 123 suggestions? I thought ^\s{1,4}\d{1,4} would narrow things down to the first 5 positions of the line, but that does not work either.

    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