Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6112449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:44:47+00:00 2026-05-23T14:44:47+00:00

i m trying to make rename program with delphi and need to know if

  • 0

i m trying to make rename program with delphi and need to know if it s possible to match some specified number of characters from the beginning, using regex.

for example if the string is FileName.txt and the specific number is 6
it should match FileNa

i also need a pattern to match string from a specific number to the end.

i would be glad if answers include descriptions because i would like to learn regex coding.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:44:48+00:00Added an answer on May 23, 2026 at 2:44 pm

    If you use Delphi XE, regular expression functionality is build in with the TRegEx class. If you use an earlier version of Delphi you can find a library here, where you can also find more about the Delphi XE support: http://www.regular-expressions.info/delphi.html

    This regular expression matches up to 6 characters until the . separating the extension from the rest of the file name.

    ^([^\.]{1,6})[^\.]*(?:\..*)?$
    

    Given the input: FileName.txt
    Group 1 would be: FileNa

    Given the input: File.txt
    Group 1 would be: File

    The expression uses grouping to capture the first 6 characters. The code in Delphi XE would look something like:

    var
        Regex: TPerlRegEx;
        ResultString: string;
    
    Regex := TPerlRegEx.Create;
    try
        Regex.RegEx := '^([^\.]{1,6})[^\.]*(?:\..*)?$';
        Regex.Options := [];
        Regex.Subject := SubjectString;
        if Regex.Match then begin
            if Regex.GroupCount >= 1 then begin
                ResultString := Regex.Groups[1];
            end
            else begin
                ResultString := '';
            end;
        end
        else begin
            ResultString := '';
        end;
    finally
        Regex.Free;
    end;
    

    For instance the filename: FileName.txt will be matched with: FileNa (group 1)

    I’ll try to explain the regular expression I have used, although there are probably better expressions out there:

    ^ # Match beginning of line
    ( # Begin a group (enables us to capture the contents alone)
      [^\.] # Capture any character that is not a '.'
      {1,6} # Capture anything from 1 to 6 of these characters (6 if possible)
    ) # Close the group
    [^\.] # Match any character that is not '.' (again)
    * # Match this 0 or more times
    (?: # Begin a group that we do not wish to capture
      \. # Capture the character '.' (the extension separator)
      .* # Capture any character 0 or more times
    ) # Close the group
    ? # Match this group 0 or 1 time (it is either there or not)
    $ # Match the end of line
    

    To the next part of your question, creating a pattern to match a string from a specific number to the end:

    ^(?:.{6})?(.*)$
    

    Given the input: This is a test
    Group 1 would be: s a test

    In this example the specific number is 6, change it to whatever number you are looking for. Again I’ve used groups to get the contents of the matched text. The first group is a none capturing group, meaning we are not interested in its content, only that we need it to be there. If we are still talking about filenames you can use the following regular expression:

    ^(?:[^\.]{6})([^\.]*)(?:\..*)?$
    

    Given the input: FileName.txt
    Group 1 would be: me

    This is a modification of the first regular expression, where I’ve made the first group none capturing, told it to be 6 characters long (again change to whatever number suits you). And excluded the extension from the captured text.

    Remember that regular expressions are easier to compose than to read. I always found that: http://www.regular-expressions.info/ is a good source of information, besides this book has helped me a great deal: Mastering Regular Expressions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, Im trying to make a program to rename some files. For the most
I'm trying to make a script to rename PC's to their serial number. I'm
I am trying make long screen to vertical direction. So, I need a screen
i'm trying to make a simple Java program to open an existing word-document, change
I'm trying to port a program from gfortran to ifort (Intel Fortran Compiler 11).
I have a lot of files I'm trying to rename, I tried to make
I'm trying to make an AppleScript droplet to rename a bunch of images annoyingly
Trying to make some small changes to Apache's Velocity engine. Here's what I can
I'm trying to make a declarative HTML Helper as specified in ScottGu's Razor post
Trying to make a make generic select control that I can dynamically add elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.