how to fetch specific text from inputed string using regex

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kdsutaia
    New Member
    • Nov 2006
    • 15

    #1

    how to fetch specific text from inputed string using regex

    Hi!
    I want to fetch "name" as per the name of the journal.

    <JOURNAL> name </JOURNAL>

    name will be diff in diff files. I am reading file line by line and sending pattern
    <JOURNAL>([-_\\w\\s])</JOURNAL> to match . it matches but it matches whole string.
    I want within ( ) bracket as in perl we can get by $1 variable.

    thanks in advance.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by kdsutaia
    Hi!
    I want to fetch "name" as per the name of the journal.

    <JOURNAL> name </JOURNAL>

    name will be diff in diff files. I am reading file line by line and sending pattern
    <JOURNAL>([-_\\w\\s])</JOURNAL> to match . it matches but it matches whole string.
    I want within ( ) bracket as in perl we can get by $1 variable.

    thanks in advance.
    You can do exactly the same in Java, i.e. $1 is the first 'capturing group' which
    means the leftmost substring in parentheses.

    kind regards,

    Jos

    Comment

    • kdsutaia
      New Member
      • Nov 2006
      • 15

      #3
      Originally posted by JosAH
      You can do exactly the same in Java, i.e. $1 is the first 'capturing group' which
      means the leftmost substring in parentheses.

      kind regards,

      Jos

      can you give emaple how to write.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by kdsutaia
        can you give emaple how to write.
        Have a look at the find() and group methods in the Matcher class.

        Alternatively use the replaceAll method in the String class; it uses
        a Pattern and a Matcher for its purpose so the replacement string can be the
        "$1" pattern.

        kind regards,

        Jos

        Comment

        Working...