How to return only the match inside a group from a regular expression?
I have a WSDL (portion show below), from which I am trying to retrieve the names of all string elements using a regular expression.
<xs:element minOccurs="0" name="appCurrDpId" type="xs:string" />
<xs:element minOccurs="0" name="appCustomerCapable" type="xs:string" />
<xs:element minOccurs="0" name="appDateReceivedSys" type="xs:date" />
<xs:element minOccurs="0" name="appDecision" type="xs:string" />
<xs:element minOccurs="0" name="appPriority" type="xs:int"/>
<xs:element minOccurs="0" name="appCountry" type="xs:string" />
So I 开发者_如何学Gohave a regex that matches the lines on which only the string elements occur:
name="(.*?)"\s?type="xs:string
But I am only interested in the portion enclosed by the first set of brackets (a group, right?), i.e. in the name attribute.
UPDATE:
The answers below have exposed a flaw in my understanding: I thought that being able to return a portion of a regular expression (as determined by a group) is a feature of a regular expression, which obviously it is not. It is more of a "side-effect", and one that requires more complex processing. In my case, I was hoping to be able to do this in my text editor (Sublime Text), because of the nifty ability to highlight and select all search results. Anyway, thanks for the answers.
As you don't say which language you're using, here a way to do it in perl:
#!/usr/bin/perl
use strict;
use warnings;
while(<DATA>) {
print $1,"\n" if /name="(.*?)"\s*type="xs:string/;
}
__DATA__
<xs:element minOccurs="0" name="appCurrDpId" type="xs:string" />
<xs:element minOccurs="0" name="appCustomerCapable" type="xs:string" />
<xs:element minOccurs="0" name="appDateReceivedSys" type="xs:date" />
<xs:element minOccurs="0" name="appDecision" type="xs:string" />
<xs:element minOccurs="0" name="appPriority" type="xs:int"/>
<xs:element minOccurs="0" name="appCountry" type="xs:string" />
output:
appCurrDpId
appCustomerCapable
appDecision
appCountry
If you put brackets around the stuff you want, you can refer to it in your replace as $1
, $2
etc for the 1st, 2nd bracketed group etc. Here's how you would do it in java:
String name = line.replaceAll("^.*name=\"(.*?)\"\\s?type=\"xs:string\".*$", "$1");
精彩评论