0
votes

I would like to match any character and any whitespace except comma with regex. Only matching any character except comma gives me:

[^,]*

but I also want to match any whitespace characters, tabs, space, newline, etc. anywhere in the string.

EDIT:

This is using sed in vim via :%s/foo/bar/gc.

I want to find starting from func up until the comma, in the following example:

func("bla bla bla"
  "asdfasdfasdfasdfasdfasdf"
"asdfasdfasdf", "more strings")

I

2
[^,]* does match until the comma? rubular.com/r/sAq4ufTDe2Oliver Atkinson
Your pattern seems to be OK. Just in case try once escaping comma, which might be required in the regexp engine you're using: [^\,]*Sina Iravanian
[^,]* matches until the comma or the end of the line for me. I was having trouble trying to get it to continue searching until it finds the comma, even if it has to go through newlinesselecsosi
I am trying to do sed in vim via :%s/foo/bar/gc. Could that be an issue?selecsosi
have you checked my suggestion?NeverHopeless

2 Answers

0
votes

To work with multiline in SED using RegEx, you should look at here.

EDIT:

In SED command, working with NewLine is a bit different. SED command support three patterns to manage multiline operations N, P and D. To see how it works see this(Working with Multiple Lines) explaination. Here these three operations discussed.

My guess is that N operator is the area of consideration that is missing from here. Addition of N operator will allows to sense \n in string.

An example from here:

Occasionally one wishes to use a new line character in a sed script. Well, this has some subtle issues here. If one wants to search for a new line, one has to use "\n." Here is an example where you search for a phrase, and delete the new line character after that phrase - joining two lines together.

(echo a;echo x;echo y) | sed '/x$/ { N s:x\n:x: }'

which generates

a xy

However, if you are inserting a new line, don't use "\n" - instead insert a literal new line character:

(echo a;echo x;echo y) | sed 's:x:X\ :'

generates

a X

y

0
votes

So basically you're trying to match a pattern over multiple lines.

Here's one way to do it in sed (pretty sure these are not useable within vim though, and I don't know how to replicate this within vim)

sed '
    /func/{
            :loop
            /,/! {N; b loop}
            s/[^,]*/func("ok"/
    }
' inputfile

Let's say inputfile contains these lines

func("bla bla bla"
  "asdfasdfasdfasdfasdfasdf"
"asdfasdfasdf", "more strings")

The output is

func("ok", "more strings")


Details
  • If a line contains func, enter the braces.
  • :loop is a label named loop
  • If the line does not contain , (that's what /,/! means)
    • append the next line to pattern space (N)
    • branch to / go to loop label (b loop)
  • So it will keep on appending lines and looping until , is found, upon which the s command is run which matches all characters before the first comma against the (multi-line) pattern space, and performs a replacement.