The best place to ask programming/development questions, imo. UPDATE: stackoverflow is the *worst* place to *ask* questions (if your first question/comment doesn't get any up-rating/response, then u can't ask anymore questions--ridiculously unrealistic), but a great reference for finding answers.

My Music (Nickleus)

20131209

regex - how to remove all characters from beginning of line, up to, and including the first space/whitespace character

today i had some console output from an error i got while debugging some code ("..." means that i've shortened the actual text bc it was so long):
12:12:54,828 INFO  [com.myapp.tiv.persistentservices.transportagreement.daoimpl....
12:13:02,216 INFO  [stdout] (http-/0.0.0.0:8080-1) setErrorMessage: Feil oppstod...
12:13:02,217 ERROR [stderr] (http-/0.0.0.0:8080-1) org.hibernate.LazyInitializat...
12:13:02,218 ERROR [stderr] (http-/0.0.0.0:8080-1)     at org.hibernate.proxy.Abstr...



so the red text is what i wanted to remove so i could use the rest of the output in an explanation in our bugtracker. to do this, i created the following regex search/replace code that i ran in "geany" (sudo apt-get install geany):
search/match:
^[^ ]+ ?(.*$)
replace:
\1


so i ended up with the following result:
INFO  [com.myapp.tiv.persistentservices.transportagreement.daoimpl....
INFO  [stdout] (http-/0.0.0.0:8080-1) setErrorMessage: Feil oppstod...
ERROR [stderr] (http-/0.0.0.0:8080-1) org.hibernate.LazyInitializat...
ERROR [stderr] (http-/0.0.0.0:8080-1)     at org.hibernate.proxy.Abstr...



at first i thought it would be good enough with just this:
search/match:
^[^ ]+ ?
replace:
(nothing)


but in geany (or is the regex just wrong?), the matching didn't stop at the end of the line so the output was like this:
 [com.takecargo.tiv.persistentservices.transportagreement.daoimpl....
 [stdout] (http-/0.0.0.0:8080-1) setErrorMessage: Feil oppstod...




EXPLANATION

^[^ ]+ ?(.*$)

first color (pink): start matching attempt at the beginning of the line
second color (purple): and match one or more characters that aren't a space (i.e. match until you meet a space)
third color (blue): and one additional single space
fourth color (aqua blue): then put everything else after that last additional space, to the end of the line, into a "capturing group" so we can reinsert it back into the line using the backreference, \1


No comments:

Post a Comment