Sponsored Content
Top Forums Programming Regular Expression Help [Java] Post 302495834 by durden_tyler on Friday 11th of February 2011 10:24:29 AM
Old 02-11-2011
Code:
$
$
$ # Display the content of the Java program
$
$ cat -n ExtractSidPid.java
     1  import java.util.regex.Matcher;
     2  import java.util.regex.Pattern;
     3  public class ExtractSidPid {
     4    public static void main(String args[]) {
     5      String str = args[0];
     6      Pattern p = Pattern.compile("sid=([^ ]+) .*pid=([^ ]+)");
     7      // Try to match the string
     8      Matcher matcher = p.matcher(str);
     9      // Display both match groups
    10      while (matcher.find()) {
    11        System.out.println("SID = " + matcher.group(1));
    12        System.out.println("PID = " + matcher.group(2));
    13      }
    14    }
    15  }
$
$
$ # Compile
$
$ javac ExtractSidPid.java
$
$ # Run the program passing a test string as argument
$
$ java ExtractSidPid "Product Name stock=6 sid=0x000000000034c000 pid=0xd70"
SID = 0x000000000034c000
PID = 0xd70
$
$ # Once more
$
$ java ExtractSidPid "Different Product Name stock=45 sid=0x000000000034c000 pid=0xa11713"
SID = 0x000000000034c000
PID = 0xa11713
$
$
$

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expression + Aritmetical Expression

Is it possible to combine a regular expression with a aritmetical expression? For example, taking a 8-numbers caracter sequece and casting each output of a grep, comparing to a constant. THX! (2 Replies)
Discussion started by: Z0mby
2 Replies

2. Shell Programming and Scripting

regular expression help

hello all.. I'm a bit new to this site.. and I hope to learn alot.. but I've been having a hard time figuring this out. I'm horrible with regular expressions.. so any help would be greatly appreciated. I have a file with a list of names like this: LASTNAME, FIRSTNAME, MIDDLEINITIAL how can... (5 Replies)
Discussion started by: mac2118
5 Replies

3. Linux

Regular expression to extract "y" from "abc/x.y.z" .... i need regular expression

Regular expression to extract "y" from "abc/x.y.z" (2 Replies)
Discussion started by: rag84dec
2 Replies

4. Programming

java regular expression

Hello all, I need to match the red expression in the following lines : MACRO_P+P-_scrambledServices_REM_PRC30.xml MACRO_P+P-_scrambledServices_REM_RS636.xml MACRO_P+P-_scrambledServices_REM_RS535.xml and so on... Can anyone give me a regular expression to match those characters ? I am... (0 Replies)
Discussion started by: lsaas
0 Replies

5. Programming

Java regular expression help

Hi, I need to write a java regular expression that has the following rules: a) should not start with space or '.' (dot) character(s) b) should not contain spaces in between of the characters c) total characters length must be less than 80 d) should not be empty string Can anyone... (0 Replies)
Discussion started by: royalibrahim
0 Replies

6. Programming

Need Java regular expression

Hi, I am looking for a Java regular expression to match the below pattern of a java script file. var so = object.device.load('camera','value'); I want to grep out such lines present in the *.js files. The conditions are: a) the line may start with blank space(s) b) always the... (0 Replies)
Discussion started by: royalibrahim
0 Replies

7. Shell Programming and Scripting

Integer expression expected: with regular expression

CA_RELEASE has a value of 6. I need to check if that this is a numeric value. if not error. source $CA_VERSION_DATA if * ] then echo "CA_RELESE $CA_RELEASE is invalid" exit -1 fi + source /etc/ncgl/ca_version_data ++ CA_PRODUCT_ID=samxts ++ CA_RELEASE=6 ++ CA_WEEK_NO=7 ++... (3 Replies)
Discussion started by: ketkee1985
3 Replies

8. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

9. Shell Programming and Scripting

Help with regular expression

I have file with following data, http://www.some.com/web11.html http://www.some.com/web/112.html http://www.some.com/web/21.html http://www.some.com/342.html http://www.some.com/plk.html http://www.some.com/abh.html http://www.some.com/yte.html http://www.some.com/tyr/098.html... (4 Replies)
Discussion started by: sol_nov
4 Replies

10. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies
guestfs-java(3) 					      Virtualization Support						   guestfs-java(3)

NAME
guestfs-java - How to use libguestfs from Java SYNOPSIS
import com.redhat.et.libguestfs.*; GuestFS g = new GuestFS (); g.add_drive ("disk.img", new HashMap<String,Object>() { { put ("readonly", Boolean.TRUE); put ("format", "raw"); } }); g.launch (); DESCRIPTION
This manual page documents how to call libguestfs from the Java programming language. This page just documents the differences from the C API and gives some examples. If you are not familiar with using libguestfs, you also need to read guestfs(3). CLOSING THE HANDLE The handle is closed when it is reaped by the garbage collector. Because libguestfs handles include a lot of state, it is also possible to close (and hence free) them explicitly by calling the "close" method. EXCEPTIONS Errors from libguestfs functions are mapped into the "LibGuestFSException" exception. This has a single parameter which is the error message (a "String"). Calling any method on a closed handle raises the same exception. EVENTS The libguestfs event API is fully supported from Java. Create a class which implements the "EventCallback" interface, create an instance of this class, and then call the "GuestFS#set_event_callback" method to register this instance. The "event" method of the class is called when libguestfs generates an event. For example, this will print all trace events: GuestFS g = new GuestFS (); g.set_trace (true); g.set_event_callback ( new EventCallback () { public void event (long event, int eh, String buffer, long[] array) { System.out.println (GuestFS.eventToString (event) + ": " + buffer); } }, GuestFS.EVENT_TRACE); g.add_drive_ro ("disk.img"); // etc. The output looks similar to this: EVENT_TRACE: add_drive_ro "disk.img" EVENT_TRACE: add_drive_ro = 0 // etc. EXAMPLE 1: CREATE A DISK IMAGE // Example showing how to create a disk image. import java.io.*; import java.util.Map; import java.util.HashMap; import com.redhat.et.libguestfs.*; public class CreateDisk { static String output = "disk.img"; public static void main (String[] argv) { try { GuestFS g = new GuestFS (); // Create a raw-format sparse disk image, 512 MB in size. RandomAccessFile f = new RandomAccessFile (output, "rw"); f.setLength (512 * 1024 * 1024); f.close (); // Set the trace flag so that we can see each libguestfs call. g.set_trace (true); // Attach the disk image to libguestfs. Map<String, Object> optargs = new HashMap<String, Object>() { { put ("format", "raw"); put ("readonly", Boolean.FALSE); } }; g.add_drive_opts (output, optargs); // Run the libguestfs back-end. g.launch (); // Get the list of devices. Because we only added one drive // above, we expect that this list should contain a single // element. String[] devices = g.list_devices (); if (devices.length != 1) throw new Error ("expected a single device from list-devices"); // Partition the disk as one single MBR partition. g.part_disk (devices[0], "mbr"); // Get the list of partitions. We expect a single element, which // is the partition we have just created. String[] partitions = g.list_partitions (); if (partitions.length != 1) throw new Error ("expected a single partition from list-partitions"); // Create a filesystem on the partition. g.mkfs ("ext4", partitions[0]); // Now mount the filesystem so that we can add files. g.mount (partitions[0], "/"); // Create some files and directories. g.touch ("/empty"); String message = "Hello, world "; g.write ("/hello", message.getBytes()); g.mkdir ("/foo"); // This one uploads the local file /etc/resolv.conf into // the disk image. g.upload ("/etc/resolv.conf", "/foo/resolv.conf"); // Because we wrote to the disk and we want to detect write // errors, call g.shutdown. You don't need to do this: // g.close will do it implicitly. g.shutdown (); // Note also that handles are automatically closed if they are // reaped by the garbage collector. You only need to call close // if you want to close the handle right away. g.close (); } catch (Exception exn) { System.err.println (exn); System.exit (1); } } } EXAMPLE 2: INSPECT A VIRTUAL MACHINE DISK IMAGE // Example showing how to inspect a virtual machine disk. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import com.redhat.et.libguestfs.*; public class InspectVM { static final Comparator<String> COMPARE_KEYS_LEN = new Comparator<String>() { public int compare (String k1, String k2) { return k1.length() - k2.length(); } }; public static void main (String[] argv) { try { if (argv.length != 1) throw new Error ("usage: InspectVM disk.img"); String disk = argv[0]; GuestFS g = new GuestFS (); // Attach the disk image read-only to libguestfs. Map<String, Object> optargs = new HashMap<String, Object>() { { //put ("format", "raw"); put ("readonly", Boolean.TRUE); } }; g.add_drive_opts (disk, optargs); // Run the libguestfs back-end. g.launch (); // Ask libguestfs to inspect for operating systems. String roots[] = g.inspect_os (); if (roots.length == 0) throw new Error ("inspect_vm: no operating systems found"); for (String root : roots) { System.out.println ("Root device: " + root); // Print basic information about the operating system. System.out.println (" Product name: " + g.inspect_get_product_name (root)); System.out.println (" Version: " + g.inspect_get_major_version (root) + "." + g.inspect_get_minor_version (root)); System.out.println (" Type: " + g.inspect_get_type (root)); System.out.println (" Distro: " + g.inspect_get_distro (root)); // Mount up the disks, like guestfish -i. // // Sort keys by length, shortest first, so that we end up // mounting the filesystems in the correct order. Map<String,String> mps = g.inspect_get_mountpoints (root); List<String> mps_keys = new ArrayList (mps.keySet ()); Collections.sort (mps_keys, COMPARE_KEYS_LEN); for (String mp : mps_keys) { String dev = mps.get (mp); try { g.mount_ro (dev, mp); } catch (Exception exn) { System.err.println (exn + " (ignored)"); } } // If /etc/issue.net file exists, print up to 3 lines. String filename = "/etc/issue.net"; if (g.is_file (filename)) { System.out.println ("--- " + filename + " ---"); String[] lines = g.head_n (3, filename); for (String line : lines) System.out.println (line); } // Unmount everything. g.umount_all (); } } catch (Exception exn) { System.err.println (exn); System.exit (1); } } } SEE ALSO
guestfs(3), guestfs-examples(3), guestfs-erlang(3), guestfs-lua(3), guestfs-ocaml(3), guestfs-perl(3), guestfs-python(3), guestfs-recipes(1), guestfs-ruby(3), http://libguestfs.org/, http://caml.inria.fr/. AUTHORS
Richard W.M. Jones ("rjones at redhat dot com") COPYRIGHT
Copyright (C) 2011-2012 Red Hat Inc. LICENSE
This manual page contains examples which we hope you will use in your programs. The examples may be freely copied, modified and distributed for any purpose without any restrictions. BUGS
To get a list of bugs against libguestfs, use this link: https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools To report a new bug against libguestfs, use this link: https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools When reporting a bug, please supply: o The version of libguestfs. o Where you got libguestfs (eg. which Linux distro, compiled from source, etc) o Describe the bug accurately and give a way to reproduce it. o Run libguestfs-test-tool(1) and paste the complete, unedited output into the bug report. libguestfs-1.22.6 2013-08-24 guestfs-java(3)
All times are GMT -4. The time now is 08:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy