java regex : getting a substring from a string which can vary

后端 未结 5 1597
借酒劲吻你
借酒劲吻你 2021-01-22 19:53

I have a String like - \"Bangalore,India=Karnataka\". From this String I would like to extract only the substring \"Bangalore\". In this case the regex

相关标签:
5条回答
  • 2021-01-22 20:26

    Try this one:

    ^(.+?)(?:,.*?)?=.*$
    

    Explanation:

    ^               # Begining of the string
      (             # begining of capture group 1
        .+?         # one or more any char non-greedy
      )             # end of group 1
      (?:           # beginig of NON capture group
        ,           # a comma
        .*?         # 0 or more any char non-greedy
      )?            # end of non capture group, optional
      =             # equal sign
      .*            # 0 or more any char
    $               # end of string
    

    Updated: I thougth OP have to match Bangalore,India=Karnataka or Bangalore=Karnataka but as farr as I understand it is Bangalore,India=Karnataka or Bangalore so the regex is much more simpler :

    ^([^,]+)
    

    This will match, at the begining of the string, one or more non-comma character and capture them in group 1.

    0 讨论(0)
  • 2021-01-22 20:32

    Try this regex, This will grab any grouping of characters at the start followed by a comma but not the comma itself.

    ^.*(?=,)

    0 讨论(0)
  • 2021-01-22 20:41
    matcher.matches()
    

    tries to match against the entire input string. Look at the javadoc for java.util.regex.Matcher. You need to use:

    matcher.find()
    
    0 讨论(0)
  • 2021-01-22 20:49

    If you are only interested to check that "Bangalore" is contained in the string then you don't need a regexp for this.

    Python:

    In [1]: s = 'Bangalorejkdjiefjiojhdu'
    
    In [2]: 'Bangalore' in s
    Out[2]: True
    
    0 讨论(0)
  • 2021-01-22 20:50

    Are you somehow forced to solve this using one regexp and nothing else? (Stupid interview question? Extremely inflexible external API?) In general, don't try to make regexes do what plain old programming constructs do better. Just use the obvious regex, and it it doesn't match, return the entire string instead.

    0 讨论(0)
提交回复
热议问题