List Info

Thread: Matching everything between two specified delimeter strings (using Java)




Matching everything between two specified delimeter strings (using Java)
country flaguser name
United States
2007-02-14 11:40:44
I have a requirement (using Java's regex framework) that can
be
characterized generally as needing to match everything
between the
(first) occurrence of two specified delimeter strings.

More specifically, I'm trying to determine the value of an
attribute
on the root tag of an XML document and would like to do so
without
having to parse the corresponding document.  So for
instance, given
the input string

<?xml version="1.0"?>
<message type="messageA">
......
</message>

I would like for my regex to return the value

messageA

The regex I'm using currently

 static Pattern messageTypePattern =
Pattern.compile("type="[^"rn]*
"");

returns in the case of the example above

type="messageA"

Can the regex be refined to match only the characters
between my
specified delimeters?


Thanks in advance for your help.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Matching everything between two specified delimeter strings (using Java)
country flaguser name
United States
2007-02-14 15:06:37
A safer way to do this (no lookaround expressions, for
example) would
be to use one regex to find the <message> start tag:

<message[^>]> or <message.*?>)

...and then a second to fish out the type attribute from
what that
gives you, e.g.,

btype="(.*?")


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


Re: Matching everything between two specified delimeter strings (using Java)
country flaguser name
United States
2007-02-14 19:04:01
On Feb 14, 12:40 pm, "kjh21" <heal...verizon.net> wrote:
> I have a requirement (using Java's regex framework)
that can be
> characterized generally as needing to match everything
between the
> (first) occurrence of two specified delimeter strings.
>
> More specifically, I'm trying to determine the value of
an attribute
> on the root tag of an XML document and would like to do
so without
> having to parse the corresponding document.  So for
instance, given
> the input string
>
> <?xml version="1.0"?>
> <message type="messageA">
> ......
> </message>
>
> I would like for my regex to return the value
>
> messageA
>
> The regex I'm using currently
>
>  static Pattern messageTypePattern =
Pattern.compile("type="[^"rn]*
> "");
>
> returns in the case of the example above
>
> type="messageA"
>
> Can the regex be refined to match only the characters
between my
> specified delimeters?
>

why not just use backreference? something like the
following:

  static Pattern messageTypePattern =
                    
Pattern.compile('type="([^"rn]*)"');
  Matcher m = messageTypePattern.matcher(input_data);
  if (m.find()) {
    String messageType = m.group(1);
  }

Dont know if the syntax is correct for Java, I don't know
Java
anyway.

Regards,
Xicheng


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )