Register to reply

Reading regular expressions

by James889
Tags: expressions, reading, regular
Share this thread:
James889
#1
Nov26-12, 02:30 PM
P: 184
Howdy,

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

' there \([^ ]*\)'
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 ?
Phys.Org News Partner Science news on Phys.org
Climate change increases risk of crop slowdown in next 20 years
Researcher part of team studying ways to better predict intensity of hurricanes
New molecule puts scientists a step closer to understanding hydrogen storage
gsal
#2
Nov26-12, 02:43 PM
P: 872
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
James889
#3
Nov28-12, 11:08 AM
P: 184
Quote Quote by gsal View Post
.. 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' ?

Ibix
#4
Nov28-12, 02:02 PM
P: 378
Reading regular expressions

"White space" would normally include tab characters. This will eat everything up until the first space character; that detail aside, yes.
gsal
#5
Nov28-12, 02:43 PM
P: 872
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.


Register to reply

Related Discussions
Regular Expressions in Python Engineering, Comp Sci, & Technology Homework 0
Regular expressions help Calculus & Beyond Homework 1
Regular Expressions and Yahoo Pipes Programming & Computer Science 2
Regular expressions and FSAs Calculus & Beyond Homework 1
Volumes of Regular Icosahedron and Regular Tetrahedron General Math 20