I need to rep...">

Matching Regex spaces in html attribute

I have a bunch of html with lines like this:

<a href="#" rel="this is a test">

I need to replace spaces in rel-attribute with underscores, but I'm kind of a regex-noob!

I am using Textmate.

Can anyone help me?

/ Jakob

+1


a source to share


6 answers


I don't think you can do it right. Although I'm wondering why you need this?

I can think of a really bad way to do this, but even if I don't recommend it, here's what:

You can do it with the regex below. However, you will need to increase the number of captures and exits with _ at the end of the potential number of spaces in rel. I bet it is a requirement that this decision prohibits.

Search:

{\<a *href\=\"[^\"]*" *rel\=\"}{([^ ]*|[^\"]*)}( |\")*{([^ ]*|[^\"]*)}( |\")*{([^ ]*|[^\"]*)}( |\")*{([^ ]*|[^\"]*)}( |\")*{([^ ]*|[^\"]*)}( |\")*{([^ ]*|[^\"]*)}( |\")*{([^ ]*|[^\"]*)}( |\")*{([^ ]*|[^\"]*)}( |\")*

      

Replace:



\1\2_\3_\4_\5_\6_\7_\8_

      

This method has two drawbacks: one may be a limitation on the number of captures you can have in Textmate, two - you end up with a large number of _ at the end of each line.

In the current test, with the regex above, you get:

<a href="#" rel="this_is_a_test">____

      

PS: This regex refers to the visual studio search / replace box format. You may need to change some of the symbols to match the text bar.

 {} => capturing group

  () => grouping

  [^A] => anything but A

  ( |\")* => space or "

  \1 => is the first capture 

      

0


a source


Let's assume you already got the rel value:



var value = document.getElementById(id).getAttribute( "rel");
var rel = (new String( value)).replace( /\s/g,"_");
document.getElementById(id).setAttribute( "rel", rel);

      

0


a source


Regexes are fundamentally bad at parsing HTML (see Can you give some examples of why it is difficult to parse XML and HTML with regex? For what). You need an HTML parser. See Can you give an example of parsing HTML with your favorite parser? for examples using various parsers.

0


a source


I have to get on board "you are using the wrong tool for the job" here. You have Textmate, so that means OSX, which means you have sed, awk, ruby ​​and perl that can do this much better and easier.

Learning how to use one of these word processing tools will give you countless benefits in the future. Here is a URL to make it easier for you to use sed: http://www.grymoire.com/Unix/Sed.html

0


a source


To find: (rel="[^\s"]*)\s([^"]*")

Replace: \1_\2

This only replaces the first white space, so click Replace All until nothing is replaced. It's not pretty, but easy to understand and work with every editor.

Change rel

in your search template if you need to clear other attributes.

0


a source


If you are using TextMate you are on Mac and therefore have Python.

Try the following:

#!/usr/bin/env python

import re

input = open('test.html', 'r')

p_spaces = re.compile(r'^.*rel="[^"]+".*$')

for line in input:
    matches = p_spaces.findall(line)

    for match in matches:
        new_rel = match.replace(' ', '_')
        line = line.replace(match, new_rel)

    print line,

      

Output example:

 $ cat test.html
testing, testing, 1, 2, 3
<a href="#" rel="this is a test">
<unrelated line>
Stuff
<a href="#" rel="this is not a test">
<a href="#" rel="this is not a test" rel="this is invalid syntax (two rels)">
aoseuaoeua

 $ ./test.py
testing, testing, 1, 2, 3
<a_href="#"_rel="this_is_a_test">
<unrelated line>
Stuff
<a_href="#"_rel="this_is_not_a_test">
<a_href="#"_rel="this_is_not_a_test"_rel="this_is_invalid_syntax_(two_rels)">
aoseuaoeua

      

-1


a source







All Articles