What Does This Regular Expression Mean?

AI Thread Summary
The discussion centers on interpreting the regular expression 'there \([^ ]*\)'. It matches the word "there" followed by a space and then captures any consecutive non-space characters. The expression effectively replaces this match with nothing, as demonstrated by the command `echo "Howdy there neighbor" | sed 's/there \([^ ]*\)//'`, which results in "Howdy". Participants clarify that the subgroup "[^ ]*" captures characters until a space is encountered, emphasizing that "blank space" refers specifically to space characters, while "white space" includes tabs and other characters. The distinction between these terms is crucial for accurate interpretation of the regular expression's behavior.
James889
Messages
190
Reaction score
1
Howdy,

I came across a regular expression i couldn't get my head around.

Code:
' there \([^ ]*\)'

Code:
echo "Howdy there neighbor" | sed 's/there \([^ ]*\)//'

returns howdy.

It's the subgroup that's a bit confusing.

match any sentence which contains banana then a space and then a non-space character.

Is this the correct way of interpreting this regular expression ?
 
Technology news on Phys.org
So, basically, it matches "there " (the word 'there' followed with a blank space) followed with as many consecutive non-blank spaces as it can find "[^ ]*" and replaces that with nothing.

You can test that only replaces what I said, if you test it with "Howdy there neighbor what up?"

Oh, the back slashes are there to escape the parenthesis within the double quotes
 
gsal said:
.. followed with as many consecutive non-blank spaces as it can find "[^ ]*" and replaces that with nothing.


Is this the same as saying 'match as much as possible up until a white space is found' ?
 
"White space" would normally include tab characters. This will eat everything up until the first space character; that detail aside, yes.
 
Sorry, I guess I need to be more correct, like Ibix says.

The expression "[^ ]*" will consume consecutive character after character until it finds a "blank space" character.

A "blank space" character itself is not the same as "white space" in general...it is a subset. If the regular expression is looking for "white space" then, "blank space" and "tab" characters qualify...but if you are looking for "blank space", then a "tab" is a totally different character.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top