English 中文(简体)
Why does this regular expression not match adjacent occurences of newline?
原标题:

I was trying to write a regexp to replace all occurrences of with unless the is already preceeded immediately by a . I m doing this in Ruby 1.8.6 which doesn t support look behind in regexps so I tried:

# try to replace 
 proceeded by anything other than 
 with 

str.gsub(/([^
])
/, "\1
") # \1 is the captured character to be kept

Given a string One Two Three the intention was for to be replaced with and the existing between Two and Three to be left unmodified. However only the first of the two match. i.e. the result is:

 "One

Two
Three"

I tried this in a couple of other regexp engines with the same result.

In the end I was able to solve this by using Oniguruma (which does support positive and negative look behind) instead of Ruby s built in regexps but I am still interested in why my alternative approach didn t work as I expected.

Thanks for any answers.

最佳回答

Just writing to explain (rsp s comment says the same thing) why your original regex didn t work. The regex engine first matches ([^ ]) at the ^ characters:

One

Two
Three
   ^^^^

After the first replacement, the regex engine is at the ^:

One

Two
Three
       ^

It s now trying to match ([^ ]) , but there is no character that is not at the caret position. So it won t match until the between Two and Three.

问题回答

You could simply replace ? with :

s = s.gsub(/
?
/, "
")

That way, all s and s are replaced by .





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签