Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Is this possible? Match exclusion

Last post 02-08-2010, 7:48 PM by daveyiv. 7 replies.
Sort Posts: Previous Next
  •  02-08-2010, 10:07 AM 59406

    Is this possible? Match exclusion

    I have a problem where I have an long string of data and contains various symbols and words and stuff. Here is an example:

    ==Title==

    body

    ===Subtitle===

    body

    ===Subtitle2===

    body

    ==Title2==

     

    I need to match the Title and Title2 but I don't want it to match the subtitles with this matcher.

     

    So I want it to match ==Word== but NOT ===Word===. Is this even possible? I am using Java if that really matters much.

     

  •  02-08-2010, 12:42 PM 59414 in reply to 59406

    Re: Is this possible? Match exclusion

    Why is it that ==Word==  is not in your sample input text? It's confusing.

    --------

    pls don't post made-up sample text, post the real text you working wth. Otherwise you'll end up getting a regex that will work OK vs your made-up input, but will fail versus your real text.

    --------

    RE:  I am using Java if that really matters much.

    regex is platform-specific, so yes people need to know in what language you code it.

  •  02-08-2010, 1:40 PM 59421 in reply to 59414

    Re: Is this possible? Match exclusion

    Word doesn't matter, it can be anything, im more concerned about matching == and not matching === at all
  •  02-08-2010, 2:00 PM 59423 in reply to 59421

    Re: Is this possible? Match exclusion

    As requested in the Posting Guidelines, please provide a sample of your actual data. Do not make up a sample.

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  02-08-2010, 2:08 PM 59424 in reply to 59423

    Re: Is this possible? Match exclusion

    ==History==
    {{Main|History of the New York Yankees}}
    ===(1901–1902) Origins: the Baltimore era===
    At the end of {{by|1900}}, Western League president [[Ban Johnson]] reorganized the league, adding teams in three Eastern cities, which formed the [[American League]]. Plans to put a team in New York City were blocked by the [[National League]]'s [[San Francisco Giants|New York Giants]], who had enough political power to keep the AL out. Instead, a team was put in [[Baltimore]], [[Maryland]], a city which had been abandoned when the NL contracted from 12 to 8 teams in 1900.
      

    This is an extract of sample data, I need ==History== to match but not the ===(1901–1902) Origins: the Baltimore era=== part

  •  02-08-2010, 2:31 PM 59425 in reply to 59424

    Re: Is this possible? Match exclusion

    give this a shot:

    (?<=[^=])==[A-Za-z]+==

    should oly match on

    ==History==

    mash would help more hopefully, he's good at java flavor.

     

     

  •  02-08-2010, 3:32 PM 59427 in reply to 59425

    Re: Is this possible? Match exclusion

    Raw Match Pattern:
    ^==[^=]+==(?!=)

    Java Code Example:

    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    class Module1{
      public static void main(String[] asd){
      String sourcestring = "source string to match with pattern";
      Pattern re = Pattern.compile("^==[^=]+==(?!=)",Pattern.MULTILINE);
      Matcher m = re.matcher(sourcestring);
      int mIdx = 0;
        while (m.find()){
          for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
            System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
          }
          mIdx++;
        }
      }
    }


    $matches Array:
    (
        [0] => Array
            (
                [0] => ==History==
            )

    )

    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  02-08-2010, 7:48 PM 59431 in reply to 59427

    Re: Is this possible? Match exclusion

    That worked, Thank you!!! :D
View as RSS news feed in XML