简体   繁体   中英

How to account for special non ASCII characters in regex

I dont know if this is the issue but I can't seem to get this to match.

String [] seTab3_HighRes=null;

public Map<String, String> tab3HighResRegex(String x, Map<String,String> map) {

Pattern Tab3_HighRes_pattern = Pattern.compile("High Resolution Parameters:(.*?Intrabolus pressure)",Pattern.DOTALL);
Matcher matcherTab3_HighRes_pattern = Tab3_HighRes_pattern.matcher(x);


while (matcherTab3_HighRes_pattern.find()) {
    System.out.println("Anything here? Nope");
    seTab3_HighRes=matcherTab3_HighRes_pattern.group(1).split("\\n|\\r");
    }
}

The text is:

 High Resolution Parameters:
    Intrabolus pressure (@LESR)(mmHg):-3.7 <8.4
    Some other stff: 123
    Intrabolus pressure (avg max)(mmHg):8.3 <17.0

I looked a bit more into the text and noticed there's a ^G character at the end of High Resolution Parameters: when I paste the text into textpad. What is it and is that the reason I'm not getting a match (and how to get rid of it?

Description

You could simply just match the ^G control G with \\cG ,

This regex does the following:

  • Matches the High Resolution Parameters:
  • finds the first Intrabolus pressure
  • pulls the substring after the Intrabolus pressure ... :

The regex

High\sResolution\sParameters:(?:\cG|[\n\r\s])*(?:Intrabolus\spressure)[^:]*:([^\n]*)

正则表达式可视化

Example

https://regex101.com/r/pE5aI0/1

Explanation

  • Capture Group 0 gets the entire string
  • Capture Group 1 gets the Intrabolus pressure value

Expanded

NODE                     EXPLANATION
----------------------------------------------------------------------
  High                     'High'
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  Resolution               'Resolution'
----------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
  Parameters:              'Parameters:'
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \cG                      ^G
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [\n\r\s]                 any character of: '\n' (newline), '\r'
                             (carriage return), whitespace (\n, \r,
                             \t, \f, and " ")
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    Intrabolus               'Intrabolus'
----------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
    pressure                 'pressure'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  [^:]*                    any character except: ':' (0 or more times
                           (matching the most amount possible))
----------------------------------------------------------------------
  :                        ':'
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [^\n]*                   any character except: '\n' (newline) (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1

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