English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What is the regular expression to insert something after the second backslash / for example 123/456/789 by using sed

2007-06-23 14:43:21 · 3 answers · asked by macausite 2 in Computers & Internet Programming & Design

3 answers

#!/bin/sh
PAT='s/\(^[^/]*\/[^/]*\/\)
/\1foo/'
# Pattern was split so Yahoo
# wouldn't truncate it.
echo $PAT
echo '123/456/789' | sed -e $PAT

======================
Output of the above shell script (pattern, followed by result):

wolf$ ./test_sed.sh
s/\(^[^/]*\/[^/]*\/\)/\1foo/
123/456/foo789
wolf$

Note that it inserts "foo" after the second slash.

======================
The pattern broken down:
\( # start a group
^ # start of line
[^/]* # any number of non-slashes
\/ # a slash
[^/]* # any number of non-slashes
\/ # a slash
\) # end of group

This matches everything in a line up to the second slash. The replacement string is:
\1 # the above group
foo # whatever text you want to insert

2007-06-23 15:24:42 · answer #1 · answered by McFate 7 · 0 0

RegEx may well be the comparable as techniques of String, and you ought to technique the text cloth besides earlier you ought to reveal. String words; Scanner sc = new Scanner( words ); StringBuilder sb = new StringBuilder(); int count style = 10; jointly as( words.hasNext() ) { count style -= words.next().length(); if( count style < 0 ) { sb.append( "n" ); count style = 10; } sb.append(words.next()); sb.append(" "); } Doing calculations with regEx is il-counseled.

2016-12-08 17:34:04 · answer #2 · answered by wingert 4 · 0 0

q346

2007-06-23 14:47:27 · answer #3 · answered by ? 3 · 0 1

fedest.com, questions and answers