English 中文(简体)
Regular expression for certain amount of characters
原标题:

I m trying to change a lot of URLs with TextWrangler, those ending with m.htm should lose the m, but only if the total number of characters in the filename is 7. Those with fewer characters should not be changed.

I ve tried

/.*?{7}m.htm/

but it doesn t work...

What is the solution?

最佳回答

Replace

href="([^"]*)/(.{6})m.htm([^"]*)"

Meaning : href=" followed by any non " character until a / (the latest the better: greedy) then 6 character followed by a m, then any non " character.

By

href="1/2.htm3"

Meaning :

1 = [^"]*
2 = .{6}
3 = [^"]*

Example

<a href="google.com/foo/bar/urzadjm.htm">testM</a> 
1 : google.com/foo/bar
2 : urzadj
3 : <empty>

If files can be htm and php, I suggest to replace .htm by (.htm|.php) (!Warning to back-references change in numbers!)

问题回答

May be

/.{6}m.htm/

That is, starting on the word boundary, followed by any 6 symbols, followed by m.htm.





相关问题
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" ...

热门标签