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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:07:47+00:00 2026-06-01T04:07:47+00:00

I’m required to do some operations which involve regular expressions. String I’m operating on:

  • 0

I’m required to do some operations which involve regular expressions.

String I’m operating on:

/dev/fd0        /media/floppy0  auto    rw,us

Basically, what I want to do is take the first two parameters (/dev/fd0 and /media/floppy0) and I want to ignore everything after this. To achieve I’ve tried the regular expressions shown below. My question is, why do the following regular expressions produce different results?

Regular expression 1:

grep -o '/dev/f\S*\s*\S*' /etc/fstab

Output (the output that I’m expecting):

/dev/fd0        /media/floppy0

Regular expression 2:

grep -o '/dev/f[\S]*\s*[\S]*' /etc/fstab

Output:

/dev/f

Regular expression 3:

grep -o '/dev/f[^\s]*\s[^\s]*' /etc/fstab

Output:

/dev/fd0        /media/floppy0  auto    rw,u

I don’t see why 2 and 3 don’t produce the same output as 1. The way I see it is that for 2, it shouldn’t matter whether I put the non white space short hand character (\S) inside a character class. The same goes for 3. Furthermore, why is 2 different from 3? Isn’t [\S] the same as [^\s]?

  • 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-01T04:07:49+00:00Added an answer on June 1, 2026 at 4:07 am

    I guess I can’t speak to whether they “should” be different — there are many regex engines where your interpretations would be correct — but in POSIX Basic Regular Expressions (BREs; the regex type that grep uses by default), [\S] is a character class containing \ and S, and [^\s] is a character class containing all characters except \ and s. (This is per the spec, which requires that, both in BREs and in EREs, “The special characters '.', '*', '[', and '\' (period, asterisk, left-bracket, and backslash, respectively) shall lose their special meaning within a bracket expression.” [link]) The within-character-class equivalent of \s is [:space:]:

    grep -o '/dev/f[^[:space:]]*\s*[^[:space:]]*' /etc/fstab
    

    Some versions of grep support a nonstandard -P option to use Perl-compatible regular expressions (PCREs) instead of POSIX regular expressions. Perl-compatible regular expressions do have the behavior you describe, so if your grep supports that option, then you can use it like this:

    grep -o -P '/dev/f[\S]*\s*[\S]*' /etc/fstab
    grep -o -P grep -o '/dev/f[^\s]*\s[^\s]*' /etc/fstab
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And

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.