2

Lets say i have a form with the following fragment:

<div class="form-control-group">
  <label class="control-label" for="FirstName">First Name</label>
  <div class="controls">
    <input id="FirstName" name="FirstName" type="text" class="input-xlarge" required="">

  </div>
</div>

<div class="form-control-group">
  <label class="control-label" for="LastName">Last Name</label>
  <div class="controls">
    <input id="LastName" name="LastName" type="text" class="input-xlarge" required="">

  </div>
</div>

I would like to extract the field names to a file so that my file looks like

FirstName
LastName

Is there a way to do this with ST3 ?

user1592380
  • 26,587
  • 62
  • 220
  • 414
  • 1
    Where there's a will there's a way. What exactly have you tried? – l'L'l Apr 11 '14 at 19:17
  • Are your FirstName and LastName inputs the only elements with the `placeholder` attribute? If so, this would be pretty simple. – CAustin Apr 11 '14 at 21:19
  • 1
    Check out the [Stack Overflow Regular Expressions FAQ](http://stackoverflow.com/a/22944075/2736496). Sublime Text uses the Boost regex engine, which, despite it's couple of backreference bugs, is pretty compatible with Perl. [Sublime regex docs](http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/search_and_replace/search_and_replace_overview.html), [Boost regex docs](http://www.boost.org/doc/libs/1_47_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html) – aliteralmind Apr 11 '14 at 21:24
  • Thanks guys, appreciate the help - Bill – user1592380 Apr 12 '14 at 00:38

1 Answers1

2

Trying to use regex with html can be a real pain — many people cringe at the thought of it; some people may even laugh at you for trying it, then direct you to the infamous question. However, that is not to say it can't be done, because it certainly can be. What it really requires more than anything is an effort to understand why html is unpredictable and how you can possibly tame it. From the looks of things you haven't even scratched the surface on that front, or even breathed on it perhaps. Fortunately as the cosmos aligned today I happened to be extracting field names from html while stumbling upon your sorrow.

Pattern:

<.*>|\n.*\s.*\sid="(\w*)".*\n+|.*>\n|\n.+

Replace:

$1

Result:

FirstName
LastName

Unless you take the time to understand the full complexity behind this somewhat simple pattern you'll likely never appreciate why you should learn what it's doing. In short the pattern finds the name of the input and puts it in a group ($1). The rest of it deals with all the unpredictability that goes with trying to use regex on html (finding <, >, carriage returns \n, spaces \s and all the other crap you don't want.

Community
  • 1
  • 1
l'L'l
  • 40,316
  • 6
  • 77
  • 124
  • Thanks, I'L'l , That's pretty close. for me it yields just LastName. I was just looking for a way to quickly rebuild forms with different fields. Maybe I should look into xpath. I've used it before but only within code, not an editor. -Bill – user1592380 Apr 12 '14 at 00:37
  • @user61629, Are you using the exact text above? If not it's probably only showing the LastName as a result of a missing carriage return perhaps. Use this example to adjust it: http://regex101.com/r/sJ1eH4 – l'L'l Apr 12 '14 at 00:48
  • I might've discovered your problem; It's the way you're using the replace command I'm thinking. If you're on OSX in your document press `Shift + Command + F` on Windows I'm not certain, but would guess it's `Shift + Alt + F` — Then Replace All. – l'L'l Apr 12 '14 at 01:01