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 8873003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:21:29+00:00 2026-06-14T18:21:29+00:00

I am having a big problem to write a regexp that will trim all

  • 0

I am having a big problem to write a regexp that will trim all the whitespace in my input.

I have tried \s+ and [ \t\t\r]+ but that don’t work.

I need this because I am writing a scanner using flex, and I am stuck at matching whitespace.
The whitespace should just be matched and not removed.

Example input:

program 
3.3 5 7 
{ comment }
string
panic: cant happen
  • 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-06-14T18:21:30+00:00Added an answer on June 14, 2026 at 6:21 pm
    1. flex uses (approximately) the POSIX “Extended Regular Expression” syntax — \s doesn’t work, because it’s a Perl extension.

    2. Is [ \t\t\r]+ a typo? I think you’ll want a \n in there.

    Something like [ \n\t\r]+ certainly should work. For example, this lexer (which I’ve saved as lexer.l):

    %{
    
    #include <stdio.h>
    
    %}
    
    %option noyywrap
    
    %%
    
    [ \n\t\r]+  { printf("Whitespace: '%s'\n", yytext); }
    [^ \n\t\r]+ { printf("Non-whitespace: '%s'\n", yytext); }
    
    %%
    
    int main(void)
    {
        yylex();
        return 0;
    }
    

    …successfully matches the whitespace in your example input (which I’ve saved as input.txt):

    $ flex lexer.l
    $ gcc -o test lex.yy.c
    $ ./test < input.txt
    Non-whitespace: 'program'
    Whitespace: ' 
    '
    Non-whitespace: '3.3'
    Whitespace: ' '
    Non-whitespace: '5'
    Whitespace: ' '
    Non-whitespace: '7'
    Whitespace: ' 
    '
    Non-whitespace: '{'
    Whitespace: ' '
    Non-whitespace: 'comment'
    Whitespace: ' '
    Non-whitespace: '}'
    Whitespace: '
    '
    Non-whitespace: 'string'
    Whitespace: '
    '
    Non-whitespace: 'panic:'
    Whitespace: ' '
    Non-whitespace: 'cant'
    Whitespace: ' '
    Non-whitespace: 'happen'
    Whitespace: '
    '
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a problem that seems easy on paper but i'm having a big problem
I think this is simple, but I am having big problem with this. I
Im experimenting a bit with jquery and having a big problem which i have
I'm having a big problem with my Lacie NAS 1TB drive, which doesn't seem
I'm having a pretty big problem trying to create navigation on my page. If
Here is a (big) example of the massive problem I am having, when this
In MVC app, I am having this big object that is used in classic
I have a big string having at most 100000 character. Instead of using string.charAt[index]
I am having a little problem. I wrote a program that extracts a set
I'm having a big problem trying to get my head round how the selectable()

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.