简体   繁体   中英

Regex to find text & value in large text

As I SSH into CM, run commands and start reading the CLI output, I get the following back:

# * A lot more output above but been removed *
terminal_output = """
[24;1H [79b[1GCommand: disp sys cust<<[23;0H[0;7m [79b[1G[0m[24;0H [79b[1G[1;0H[0;7m [79b[1G[0m[2;0H [79b[1G[3;1H[0J7[1;1H[0;7mdisplay system-parameters customer-options [0m8[1;65H[0;7mPage   1 of  12[0m[2;33HOPTIONAL FEATURES[4;8HG3 Version: [4;20HV20 [4;50HSoftware Package: [4;68HEnterprise  [5;10HLocation: [5;20H2[6;10HPlatform: [6;20H28   [5;51HSystem ID (SID): [5;68H9990093751   [6;51HModule ID (MID): [6;68H1     [8;60HUSED[9;29HPlatform Maximum Ports: [9;53H 81000[9;60H   436[10;35HMaximum Stations: [10;53H   135[10;60H   110[11;27HMaximum XMOBILE Stations: [11;53H 41000[11;60H     0[12;17HMaximum Off-PBX Telephones - EC500: [12;53H   135[12;60H     2[13;17HMaximum Off-PBX Telephones -   OPS: [13;53H   135[13;60H    40[14;17HMaximum Off-PBX Telephones - PBFMC: [14;53H   135[14;60H     0[15;17HMaximum Off-PBX Telephones - PVFMC: [15;53H   135[15;60H     0[16;17HMaximum Off-PBX Telephones - SCCAN: [16;53H     0[16;60H     0[17;22HMaximum Survivable Processors: [17;53H 313[17;62H   1[22;9H(NOTE: You must logoff & login to effect the permission changes.)[2;50H[0m
"""

It's a lot of ANSI escape codes (I think?) which sort of makes the output not too readable but anyways, what I'm trying to get back is the following from the text above:

Maximum Stations: 135 110

I know from my understanding that a Regex would be required for this. The Regexes that I tried using but did not work:

r'Maximum Stations:\s*(\d+)(\d+)'
r'Maximum Stations: \d+'

If anyone knows how to filter out these ANSI character codes so they don't appear in the final output that'd be great too.

Thank you.

you can try the following

"(Maximum Stations:)\s\[\d*;\d*H\s*(\d*)\[\d*;\d*H\s*(\d*)"gm

it produces three groups the first with the maximum stations text then two more each with the number you wanted to capture. You would have to combine the groups to get your final output.

I don't know if this will be generic enough for your application though.

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