Reputation: 36327
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 ?
Upvotes: 3
Views: 14106
Reputation: 47284
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.
Upvotes: 3