Ruby(Rails)gsub:将捕获的字符串传递给方法(Ruby (Rails) gsub: pass the captured string into a method)
我试图匹配一个字符串:
text = "This is a #hastag" raw( h(text).gsub(/(?:\B#)(\w*[A-Z]+\w*)/i, embed_hashtag('\1')) ) def embed_hashtag('data') #... some code to turn the captured hashtag string into a link #... return the variable that includes the final string end
我的问题是,当我在使用gsub调用的embed_hashtag方法中传递
'\1'
时,它直接传递"\1"
,而不是从我的正则表达式中第一个捕获的组。 有其他选择吗?供参考:
我在
h
包装文本以转义字符串,但后来我将代码嵌入用户输入的文本(即标签),这需要通过原始(因此是raw
)传递。将“#”符号与文本分开很重要,这就是为什么我相信我需要捕获组。
如果你有更好的方法来做到这一点,不要犹豫,让我知道,但如果有人有这个问题,我仍然希望能回答这个问题。
I'm trying to match a string as such:
text = "This is a #hastag" raw( h(text).gsub(/(?:\B#)(\w*[A-Z]+\w*)/i, embed_hashtag('\1')) ) def embed_hashtag('data') #... some code to turn the captured hashtag string into a link #... return the variable that includes the final string end
My problem is that when I pass
'\1'
in my embed_hashtag method that I call with gsub, it simply passes"\1"
literally, rather than the first captured group from my regex. Is there an alternative?FYI:
I'm wrapping text in
h
to escape strings, but then I'm embedding code into user inputted text (i.e. hashtags) which needs to be passed raw (henceraw
).It's important to keep the "#" symbol apart from the text, which is why I believe I need the capture group.
If you have a better way of doing this, don't hesitate to let me know, but I'd still like an answer for the sake of answering the question in case someone else has this question.
原文:https://stackoverflow.com/questions/24230877
最满意答案
代替
schemaExport.create( true, false );
呼叫
schemaExport.execute(true, false, false, true)
它将创建没有alter / drop语句的DDL
(有关详细信息,请参阅javadoc http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/tool/hbm2ddl/SchemaExport.html )
Instead of
schemaExport.create( true, false );
call
schemaExport.execute(true, false, false, true)
It will create DDL without alter/drop statements
(see javadoc for details http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/tool/hbm2ddl/SchemaExport.html)
相关问答
更多-
用hibernate.hbm2ddl.import_files执行sql脚本时出错?(Error executing sql script with hibernate.hbm2ddl.import_files?)[2024-03-07]
尝试用单引号替换doulbe引用如下: insert into Borrower values (1,'Greece, Aphines, Square street',5,'Antonio'); Try it with replace doulbe quote by single quote as below: insert into Borrower values (1,'Greece, Aphines, Square street',5,'Antonio'); -
您需要区分验证api和java持久性api(jpa)(以及供应商特定的持久性api)。 Hibernate考虑JPA配置(和hibernate持久性api),当你不提供这样的配置时, Convention Over Configuration原则就涉及到这个过程。 这就是为什么你得到varchar(255) @Size(max = 50) private String shortTitle; 它等于(我省略了其他默认值) @Size(max = 50) @Column(length = 255, null ...
-
没有合适的司机。(No suitable driver. Trying to connect to a postgresql database on Heroku using Hibernate)[2024-04-17]
你的数据库URL有一些可疑的东西: JDBC:在PostgreSQL:// paafeblgkhhbkx:PhZEtHl2RHwbWDQJJOurmej89-@ec2-54-243-243-252.compute-1.amazonaws.com:5432 / d1vsgjgnfr1koq 分解: JDBC方案: postgresql - 确定 主机名: paafeblgkhhbkx - 可疑 port: PhZEtHl2RHwbWDQJJOurmej89-@ec2-54-243-243-252.comput ... -
user是postgresql中的保留关键字 。 我建议将实体类重命名为其他名称,或者只使用hibernate注释 @Table指定postgresql友好表名,如下所示: @Entity @Table(name = "library_user") public class User {...} user is a reserved keyword in postgresql. I would suggest renaming your entity class to some other name, o ...
-
首先,您应该删除数据库或更改您的hibernate配置以进行创建
create 而错误是 ERROR: relation "user_roles" does not exist 所以你应该将ManyToMany命名为user_roles : @JoinTable(name="user_roles", joinColumns = {@JoinColumn(name="user_id", referenced ... -
Hibernate“hbm2ddl - create”在春天不起作用(Hibernate “hbm2ddl - create” does not work in spring)[2023-11-15]
您缺少@Table注释 @Entity @Table(name = "User") public class User{ ..... You are missing the @Table annotation @Entity @Table(name = "User") public class User{ ..... -
org.hibernate.tool.hbm2ddl.SchemaExport和PostgreSQL(org.hibernate.tool.hbm2ddl.SchemaExport and PostgreSQL)[2022-07-18]
代替 schemaExport.create( true, false ); 呼叫 schemaExport.execute(true, false, false, true) 它将创建没有alter / drop语句的DDL (有关详细信息,请参阅javadoc http://docs.jboss.org/hibernate/orm/3.3/api/org/hibernate/tool/hbm2ddl/SchemaExport.html ) Instead of schemaExport.create ... -
在hibernate工具hbm2ddl中支持@ElementCollection(Support for @ElementCollection in hibernate tools hbm2ddl)[2022-05-05]
我认为你的插件版本不匹配。 hibernate3-maven-plugin依赖于Hibernate 3.3.1.GA. 您可以尝试在hibernate3-maven-plugin上忽略它,并手动获取正确的版本(3.5.3 Final可能有效)。 也许在字段而不是setter上设置注释也可能有所帮助。 I think you just have a version mismatch with the plugin. hibernate3-maven-plugin has a dependency on Hib ... -
我的错误是hibernate属性中的位置。 hibernate.hbm2ddl.import_files = /META-INF/spring/import.sql 是正确的位置。 My error was the location in hibernate properties. hibernate.hbm2ddl.import_files = /META-INF/spring/import.sql is the correct location.
-
hbm2ddl.auto验证:如果值为validate,则hibernate仅验证表和列是否存在。 如果该表不存在,那么hibernate会抛出异常。 Validate是hbm2ddl.auto的默认值。 hbm2ddl.auto update:如果值是update,那么hibernate会检查表和列。 如果表不存在则会创建一个新表,如果列不存在,则会为其创建新列。 此链接对所有值都有漂亮的解释。 http://www.onlinetutorialspoint.com/hibernate/hbm2ddl-a ...