简体   繁体   中英

Java regex for string pattern

I would want to write a regex for this string pattern:

<Col name="SKU_UPC_NBR">85634546495</Col>

I want to fetch the value between Col tag.

I tried the below pattern :

Pattern TAG_REGEX = Pattern.compile("<Col name='SKU_UPC_NBR'>(.+?)</col>");
Matcher matcher = TAG_REGEX.matcher(str);

The above is not matching my string and returns empty. Please help me on this problem.

You can try:

<Col[^>]*>(.+?)<\/Col>
  1. <Col[^>]*> will match the opening tag. [^>]* means match any character but >, so that the match ends at the first > encountered.
  2. (.+?) means grab 1 or more characters between the opening and closing tag
  3. <\\/Col> this matches the closing tag

Try this please:

(?<=">)\d*(?=<\/)

It will match 0 or more digits preceded by "> (quotation mark and greater than sign) and followed by (less than sign and forward slash)

You can test this here:

https://regex101.com/

Regex matches exactly what you type. It does not generalize, it does not understand that sometimes to you ' == " , it does not match mixing cases.

The data format you've specified is open tag, space, name attribute, equals, double quote , name attr data ...
The regex format you've specified is open tag, space, name attribute, equals, single quote , name attr data ...

What you need is

Pattern TAG_REGEX = Pattern.compile("<Col name=\"SKU_UPC_NBR\">(.+?)</Col>");

NOTE: You may want to use (\\d+?) instead of (.+?) as \\d will match any digit, so the regex is more specific to the data you're matching, and is easier to read. This won't work however, if you know some Col tags won't have just digits in them


You may want to refer to this neat interactive Regex tutorial for practice with regex's.

You also may want to refer to the Java documentation for Regex patterns , this is useful when you need special characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM